0

I save an array that contains some translation in text file. (.txt)

array structure :

$translate = array(
  "Search. Discover. Book Instantly" => "بگرد مقایسه کن رزرو کن",
  '24/7 Access to top salon' => ' خیلی خوب خیلی عالی در 7 روز هفته',
  'EASY AND SIMPLE'=> 'راحت و ساده',
); 

now I want to add new fields to array but I don't know how to do it, I need something like array_push() for add keys and value both.

Example:

$new_line = array("Hello World" => "سلام دنیا");

// add to main array
$translate = array(
  "Search. Discover. Book Instantly" => "بگرد مقایسه کن رزرو کن",
  '24/7 Access to top salon' => ' خیلی خوب خیلی عالی در 7 روز هفته',
  'EASY AND SIMPLE'=> 'راحت و ساده',
  "Hello World" => "سلام دنیا"
); 
1
  • You can do some this like this $translate['Hello World'] = "سلام دنیا"; Commented Apr 27, 2017 at 11:44

6 Answers 6

3

since $new_line is an array, you probably want to merge them

array_merge($array1, $array2)

read more here http://php.net/manual/en/function.array-merge.php

Sign up to request clarification or add additional context in comments.

Comments

1
$translate = array(
  "Search. Discover. Book Instantly" => "بگرد مقایسه کن رزرو کن",
  '24/7 Access to top salon' => ' خیلی خوب خیلی عالی در 7 روز هفته',
  'EASY AND SIMPLE'=> 'راحت و ساده'
); 

$translate["Hello World"] = "سلام دنیا";

Or if you have a separate array with new values

$new_line = array("Hello World" => "سلام دنیا", "Hello World1" => "سلام دنیا");

$translate = array_merge($translate, $new_line);

Comments

0

This does the same as an array push

$translate[] = [
    'Hello World' => 'سلام دنیا'
];

2 Comments

You should note that this will work on php 7. Earlier version use array() instead of []
Thanks @JurijJazdanov your right, my mistake for assuming PHP version. all good, RiggsFolly is gonna get the top answer anyway.
0

You have to do it like that

$translate['Hello World'] = "سلام دنیا";

There is no array_push() equivalent for associative arrays because there is no way determine the next key.

4 Comments

No you have to specify key and there value. $translate['key'] = "value";
that's not true. It will use the key as a number and increment it. Please don't misinform if you are not sure
you are assuming that $translate[] = 'value' wont work. That's incorrect and it is a equivalent to array_push(), which u said is not.
I mentioned $translate['key'] = "value" @JurijJazdanov
0

simply add a new entry

<?php

$new = array("Hello World" => "سلام دنیا");

// add to main array
$translate = array(
  "Search. Discover. Book Instantly" => "بگرد مقایسه کن رزرو کن",
  '24/7 Access to top salon' => ' خیلی خوب خیلی عالی در 7 روز هفته',
  'EASY AND SIMPLE'=> 'راحت و ساده',
  "Hello World" => "سلام دنیا"
); 

/* single entry * /
$translate['new'] = $new[0];
unset($new[0]);
/**/

//several entries
//array_merge($translate,$new);

//or with iteration
foreach($new as $k => $v){
    //here you could add some conditions on what to add and what not
    $translate[$k]=$v;
    unset($new[$k]);
};

print_r( $translate );

?>

maybe you do not need unset. also have a look at find the key for associative array entries.

Comments

0

With the use of the key() array function it can be done like this.

$translate = array(
  "Search. Discover. Book Instantly" => "بگرد مقایسه کن رزرو کن",
  '24/7 Access to top salon' => ' خیلی خوب خیلی عالی در 7 روز هفته',
  'EASY AND SIMPLE'=> 'راحت و ساده',
); 

$new_line = array("Hello World" => "سلام دنیا");

$translate[key($new_line)] = $new_line[key($new_line)];

print_r($translate);

Result

Array
(
    [Search. Discover. Book Instantly] => بگرد مقایسه کن رزرو کن
    [24/7 Access to top salon] =>  خیلی خوب خیلی عالی در 7 روز هفته
    [EASY AND SIMPLE] => راحت و ساده
    [Hello World] => سلام دنیا
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.