3

This is my code, in this code, I am reading an existing array through a function read_from_json, which convert JSON to array, now from remote data I am getting new data so I have to append those data in my existing array without overwriting the whole array. Like if I am getting an id, it searches using the in_array function, if it is not found then sending a message to it, and then append the only specific entry to existing array. There is a problem due to foreach iteration so it overwrites all array, what else can I do. Please have a look at this code:

$read_data = $this->read_from_json( 'xyz.json' );

foreach ( $projects_id_tickcamp as $tick_id => $base_id ) {

            if ( !$this->in_array( $base_id['base_id'], $read_data ) ) {

                echo '<b>do post message function for ' . $tick_id . ' ' . $base_id['base_id'] . '</b><br />';

                $i = count( $read_data );

                while ( $i >= count( $base_id['base_id'] ) ) {

                    echo 'post message start' .'<br />';
                    $i++;
                    break;
                    $projects_id_tickcamp[$tick_id]['message_id'] = 1;

                }

                //echo 'posted message id of ' . $base_id['basecamp_id'] . '<br />';
            } else {
                echo 'do nothing' . '<br />';
                //return false;
            }
        }

        //echo 'write data if id similar' . '<br />';
        $this->write_to_json( 'xyz.json', $projects_id_tickcamp );
        return $projects_id_tick;

The output of the above code looks like:

Array
(
    [125434] => Array
        (
            [base_id] => 1306755
        )

    [127354] => Array
        (
            [base_id] => 1287834
        )

)

if a new id fetch from remote then id writes only in last place of array.

1

3 Answers 3

13

You have a few options:

Good luck!

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

5 Comments

i am using array_merge but it reindexing array keys, and i want want my keys as it is unique id's. On the other hand if i am using array_push it works but create new array itself with different key and add element which i dont want
Then why not do $existingArray[ $newKey ] = $newValue
See i am iterating my existing array and comparing it against for new element, if there is new element then i want to append to it, and it appends but overwrite previous one elements
Heyy i am doing some stuff in my code and simple array[] is worked for me.
$array[] = $newValue seemed illogical to me, but ChatGPT assured me it it is true. I came here to get a second opinion.. looks like I got the human backup I needed, thanks Barry.
3

after returning another value,using array_merge will fix this.

example:

$result_array=array_merge($arr1,$arr2);

4 Comments

should i create another array to store latest id array and then merge it with former array?
yes.array_merge function takes input as only arrays.
hey thanks, it works for me, but it takes some time, i have to optimize it for fast execution. Atleast overwrite problem is solved.
Hey, array merge reindexing keys, but i need my keys, its unique id which is necessary for further work
0

If you want to append something to a PHP array, you can use $myArray[] = "new value"

2 Comments

previous array made through this: $projects_id_tickcamp[$tick_id]['base_id'] = value;, if i got new id then i have to append it through $projects_id_tickcamp[$tick_id]['basecamp_id'] = new value, but foreach overwrites all array
@shihon then dont overwrite the array and use the old one. just append when the array_key does not exist

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.