PHP array merge keys [duplicate] - php

This question already has answers here:
How to get an array of specific "key" in multidimensional array without looping [duplicate]
(4 answers)
Closed 8 years ago.
In PHP, is there any built-in array function that I could use to change an array...
From:
Array (
[0] => Array
(
[item_id] => 1
)
[1] => Array
(
[item_id] => 3
)
[2] => Array
(
[item_id] => 2
)
)
To:
Array
(
[item_id] => Array ( [0] => 1, [1] => 2, [2] => 3 )
)
Thanks for your help.

Even if you cant find a built-in function, you can write a small loop. Although im sure this can also be done with array_map
foreach($yourArray as $subArray)
{
$newArray["item_id"][]=$subArray["item_id"];
}

Related

PHP : Sort an array by value based on another array [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Sorting a php array of arrays by custom order
(8 answers)
Closed last month.
I have two array and I want to arrange one of them based on the other one.
$sortMe = Array
(
[0] => 100
[1] => 1
[2] => 10
[3] => 1000
)
$example = Array
(
[0] => 1
[1] => 1000
[2] => 100
[3] => 10
)
how can I sort the $sortMe array based on $example to:
$sortMe = Array
(
[1] => 1
[3] => 1000
[0] => 100
[2] => 10
)

How can I get multiple values from array within array? [duplicate]

This question already has answers here:
How to extract specific array keys and values to another array?
(2 answers)
Closed 2 years ago.
I have an array that looks like this:
[meta_data] => Array
[0] => Array
(
[id] => 100
[name] => John
)
[1] => Array
(
[id] => 200
[name] => Peter
)
[2] => Array
(
[id] => 300
[name] => Peter
)
What would be the simplest way to select all names from $meta_data?
Thanks.
see https://www.php.net/manual/en/function.array-column.php
var_dump(array_column($array['meta_data'], 'name'));

Comma separated value from array [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
PHP - Multidimensional array to CSV
(3 answers)
How to write a multidimensional array to a csv file. PHP
(2 answers)
multidimensional irregular array to CSV in PHP
(1 answer)
Closed 3 years ago.
I need to explode stdentid array in comma separated value.currently the studentid array is in multidimensional array i need all the value should be in comma separated
The result should be for studentid array is
[studentid] => 36399,96500,96503,96509,96512 and so on..
Array
(
[started] => 1
[studentid] => Array
(
[1] => Array
(
[0] => 36399
)
[2] => Array
(
[0] => 96500
[1] => 96503
[2] => 96506
[3] => 96509
[4] => 96512
[5] => 96515
)
[3] => Array
(
[0] => 96501
[1] => 96504
[2] => 96507
[3] => 96510
[4] => 96513
)
[4] => Array
(
[0] => 96502
[1] => 96505
[2] => 96508
[3] => 96511
[4] => 96514
)
)
[name] => Test
[name_or_email] =>
[submitbtn] => 1
)
assuming your variable is $studentsResult (which contains the whole array you have posted)
in that case:
foreach($studentsResult['studentid'] as $studentId => $studentValues){
$tempStudents[$studentId] = join(',',$studentValues);
}
and then $tempStudents is what you're probably looking for\
If, however all those IDs needs to be under that ONE student, then you haven't said where is the ID, but you can follow the answer on flattening the array in one of the comments or do:
$tempStudents = [];
foreach($studentsResult['studentid'] as $someId => $studentValues){
$tempStudents = array_merge($tempStudents,$studentValues);
}
echo join(',',$tempStudents);

How can I convert this array to an object? [duplicate]

This question already has answers here:
How to convert an array to object in PHP?
(35 answers)
Closed 8 years ago.
How can i convert an array like this to object?
Array
(
[max] => Array
(
[0] => 21.
[1] => male.
[2] => UK.
)
[alex] => Array
(
[0] => 20.
[1] => male.
[2] => sweden.
)
[ali] => Array
(
[0] => 20.
[1] => male.
[2] => saudi arabia.
)
)
i've alrady tried for each loop and it didn't work.
is there any way to convert it ?
Try this.
$object = json_decode(json_encode($array));
or you can also do this.
$object = (object) $array;
Here's a demo.

How to display the values of array in sequence in PHP? [duplicate]

This question already has answers here:
PHP reindex array? [duplicate]
(4 answers)
How do you reindex an array in PHP but with indexes starting from 1?
(12 answers)
Closed 9 years ago.
I need to display array in sequence
my array
Array
(
[0] => test#yahoo.com
[1] => rans#yahoo.com
[2] => maria#gmail.com
[3] => lspap#gmail.com
[6] => sage#yahoo.com
[7] => rlope#hotmail.com
[13] => alyssa#gmail.com
)
I want output like following
Array
(
[0] => test#yahoo.com
[1] => rans#yahoo.com
[2] => maria#gmail.com
[3] => lspap#gmail.com
[4] => sage#yahoo.com
[5] => rlope#hotmail.com
[6] => alyssa#gmail.com
)
Now my question is how can display array like above sequence in PHP?
use array_values to reindex an array
$array = array_values($array);
To display the values in sequence use a foreach loop as so
foreach($array as $arr){
echo $arr."\n";
}
to store the values with new indexes or keys use
array_values($array);

Categories