How to add to an array in a loop - php

I’m trying to add to an array in a loop but only the first element in the loop is added.
The array
array (size=7)
0 =>
array (size=2)
'id' => int 1
'name' => string 'john' (length=11)
1 =>
array (size=2)
'id' => int 2
'name' => string 'adam' (length=13)
2 =>
array (size=2)
'id' => int 3
'name' => string 'mary' (length=11)
My loop
foreach ($loops as $key => $loop) {
$idArray['id'] = $loop['id'];
}
var_dump($idArray); die();
Did I do anything wrong?

You overwrite your old value by assigning the new value to the array. An array can't have identical keys.
Try this:
foreach ($loops as $key => $loop)
{
$idArray['id'][] = $loop['id'];
}
var_dump($idArray); die();
So you add items to an array inside your array.

If you want the ordinal values of the $idarray to be the primary key values of what you are iterating over you can do this.
$loops = array(array('id' => 1, 'name' => 'john'), /* ... */);
foreach ($loops as $key => $loop)
{
$idArray[$loop['id']] = $loop;
}
var_dump($idArray); die();
var_dump will reveal this structure
array (size=7)
1 =>
array (size=2)
'id' => int 1
'name' => string 'john' (length=4)
2 =>
array (size=2)
'id' => int 2
'name' => string 'adam' (length=4)
3 =>
array (size=2)
'id' => int 3
'name' => string 'mary' (length=4)

Related

How to merge array key in php

array (size=1)
11 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '21' (length=2)
array (size=1)
10 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '10' (length=2)
array (size=1)
11 =>
array (size=1)
'subject' =>
array (size=1)
'Gujarati' => string '21' (length=2)
array (size=1)
10 =>
array (size=1)
'subject' =>
array (size=1)
'Gujarati' => string '20' (length=2)
array key duplicate like 11 and 10
MY Question:
How to Create Below output array .
array (size=1)
11 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '21' (length=2)
'Gujarati' => string '21' (length=2)
array (size=1)
10 =>
array (size=1)
'subject' =>
array (size=1)
'Maths' => string '10' (length=2)
'Gujarati' => string '20' (length=2)
Use this function to get your result.
Btw: Just asking for help again and again without investigating by yourself is bad behavior, if someone points you to a function (like array_merge_recursive) you should at least take some time to RTM.
function array_merge_recursive_new() {
$arrays = func_get_args();
$base = array_shift($arrays);
foreach ($arrays as $array) {
reset($base); //important
while (list($key, $value) = #each($array)) {
if (is_array($value) && #is_array($base[$key])) {
$base[$key] = array_merge_recursive_new($base[$key], $value);
} else {
$base[$key] = $value;
}
}
}
return $base;
}
Source/Copyright:
http://php.net/manual/en/function.array-merge-recursive.php#106985
I have tested it with these arrays, which should be the same as you have them.
$arr1 = array(
10 => array( "subject" => array("math" => 1)),
11 => array( "subject" => array("math" => 2)),
);
$arr2 = array(
10 => array( "subject" => array("guawhatever" => "foo")),
11 => array( "subject" => array("guawhatever" => "bar")),
);
$blubb = array_merge_recursive_new($arr1,$arr2);

PHP renaming array and covert from string to integer

I have var_dump() variable $fruit_array as below:
array (size=3)
0 =>
array (size=1)
'fruit_id' => string '2' (length=1)
1 =>
array (size=1)
'fruit_id' => string '1' (length=1)
2 =>
array (size=1)
'fruit_id' => string '3' (length=1)
I need to rename fruit_id to id and convert array string value to integer, example result:
array (size=3)
0 =>
array (size=1)
'id' => int 2
1 =>
array (size=1)
'id' => int 1
2 =>
array (size=1)
'id' => int 3
How can I do that ? thanks
You can do something like this:
<?php
foreach ( $array as $k=>$v )
{
$array[$k] ['id'] = intval( $array[$k] ['fruit_id'] );
unset($array[$k]['fruit_id']);
}

Mimicking a join and group by statement with multidimensional array

Source/Input
I have a multidimensional array:
array (size=2)
'array_one' =>
array (size=2)
0 =>
array (size=2)
0 => string 'ABC' (length=3)
1 => string 'ABC' (length=3)
'myid' =>
array (size=2)
0 => string 'ABC' (length=3)
1 => string 'ABC' (length=3)
'array_two' =>
array (size=2)
0 =>
array (size=2)
0 => string 'DEF' (length=3)
1 => string 'DEF' (length=3)
'myid' =>
array (size=2)
0 => string 'DEF' (length=3)
1 => string 'DEF' (length=3)
i want to combine the arrays but if an array element's key is the same i want to replace both element with a new element with a new (dynamically created using it's parent) name, i want the output to look something like this:
Expected Output
array (size=1)
'new_array' =>
array (size=2)
'myid_array_one' =>
array (size=2)
0 => string 'ABC' (length=3)
1 => string 'ABC' (length=3)
'myid_array_two' =>
array (size=2)
0 => string 'DEF' (length=3)
1 => string 'DEF' (length=3)
Method
This is what i have tried:
public function data_combine($data_set_arrays) {
$result = [];
$accociative = $data_set_arrays;
foreach($accociative as &$array) {
foreach($array as $key => $value) {
if(is_int($key)) {
unset($array[$key]);
}
}
}
foreach($accociative as $array) {
//make sure no elements have same name
$parent = key($array);
foreach($array as $key => $value) {
$duplicates = array_intersect_key($data_set_arrays, $array); {
foreach($duplicates as $key => $value) {
$array[$key.'_'.$parent] = $array[$key];
unset($array[$key]);
}
}
}
$result = array_merge_recursive($result, $array);
}
return $result;
}
but i can't see the wood for the trees, can someone lend a hand?

Removing an item from a multidimensional array in php by value

I have a multidimensional array $array that looks like this:
array (size=3)
0 =>
array (size=1)
0 =>
object(stdClass)[500]
public 'id' => int 2
public 'first_name' => string 'Mary' (length=4)
public 'last_name' => string 'Sweet' (length=5)
1 =>
array (size=1)
0 =>
object(stdClass)[501]
public 'id' => int 9
public 'first_name' => string 'Joe' (length=3)
public 'last_name' => string 'Bob' (length=3)
2 =>
array (size=1)
0 =>
object(stdClass)[502]
public 'id' => int 1
public 'first_name' => string 'Shag' (length=4)
public 'last_name' => string 'Well' (length=4)
I would like to be able to remove one of the items in the array by searching for values (not indexes).
So, I would like to remove the item in the array that has the first_name property of 'Joe'.
So if I removed it, the array would look like this:
array (size=2)
0 =>
array (size=1)
0 =>
object(stdClass)[500]
public 'id' => int 2
public 'first_name' => string 'Mary' (length=4)
public 'last_name' => string 'Sweet' (length=5)
1 =>
array (size=1)
0 =>
object(stdClass)[502]
public 'id' => int 1
public 'first_name' => string 'Shag' (length=4)
public 'last_name' => string 'Well' (length=4)
How would I be able to do this? Thanks.
Yes you could just use a foreach in this case. It will work just fine. Just use your search string needle and add an if inside the loop comparing the objects property firstname and the needle:
$first_name_search = 'Joe';
foreach ($array as $key => $value) {
$value = reset($value); // get that index 0 which is nested
if($value->first_name == $first_name_search) {
unset($array[$key]);
}
}

Array 1st value as a key on new array and second as its values

I have a array:
$array1 = array
0 =>
array
0 => string 'biodata' (length=7)
1 => string 'family_name' (length=11)
1 =>
array
0 => string 'biodata' (length=7)
1 => string 'first_name' (length=10)
2 =>
array
0 => string 'biodata_education' (length=17)
1 => string 'subject' (length=7)
3 =>
array
0 => string 'biodata_education' (length=20)
1 => string 'year' (length=5)
which need to converted like:
array
biodata =>
array
0 => string 'family_name' (length=7)
1 => string 'first_name' (length=11)
biodata_education =>
array
0 => string 'subject' (length=7)
1 => string 'year' (length=10)
as it can be done by simple iteration, I tried this one and done.
foreach($array1 as $tbl):
$table[$tbl[0]][] = $tbl[1];
endforeach;
<?php
//map the array using a foreach loop
foreach($array1 as $tbl)
{
$table[ $tbl[0] ][] = $tbl[1];
}

Categories