I have an multidimensional array but need to make it smaller.
This is an easy question I believe. I need to remove 1 array in
jsonresult array, the first one but preserve the other in the next array. I have tried array_splice but it only keeps one.
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array // THIS ONE, KEEP ITS CHILDREN MOVE UP
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14389720
)
[2] => Array
(
[id] => 14869723
)
)
[1] => Array // THIS ONE, KEEP ITS CHILDREN MOVE UP
(
[0] => Array
(
[id] => 14889722
)
[1] => Array
(
[id] => 14389711
)
[2] => Array
(
[id] => 14869329
)
)
)
)
Would like to get:
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14389720
)
[2] => Array
(
[id] => 14869723
)
[3] => Array
(
[id] => 14889722
)
[4] => Array
(
[id] => 14389711
)
[5] => Array
(
[id] => 14869329
)
)
)
Try this code. This may not be the correct method but it gives what you need. (As I understand from your question)
//creating a sample array similar to one you given in question.
$arr_test['searchword'] = 'search word';
$arr_test['jsonresult'] = array(array(array('id'=>14889770),array('id'=>14889720)),array(array('id'=>14889780),array('id'=>14889790)));
//creating new array
$arr_new = array();
//formatting array as you needed it
foreach($arr_test['jsonresult'] as $arr_jsonresult){
foreach($arr_jsonresult as $jsonresult){
$arr_new['jsonresult'][] = $jsonresult;
}
}
//overwriting the specific array key
$arr_test['jsonresult'] = $arr_new['jsonresult'];
//checking output
echo '<pre>';
print_r($arr_test);
This code produces the following output
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14889720
)
[2] => Array
(
[id] => 14889780
)
[3] => Array
(
[id] => 14889790
)
)
)
Related
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 months ago.
I have below array with multi dimensional array. How I can convert it into one dimensional array.
Array
(
[0] => Array
(
[id] => 21134
)
[1] => Array
(
[id] => 21135
)
[2] => Array
(
[id] => 21137
)
[3] => Array
(
[id] => 21138
)
[4] => Array
(
[id] => 21144
)
[5] => Array
(
[id] => 21147
)
[6] => Array
(
[id] => 21149
)
[7] => Array
(
[id] => 21150
)
[8] => Array
(
[id] => 21151
)
[9] => Array
(
[id] => 21152
)
[10] => Array
(
[id] => 21153
)
)
This case array_column() is very useful:
$resultArray = array_column($yourArray,'id');
If will give you the values of all the id's in a one-dimensional array.
See: http://php.net/manual/en/function.array-column.php
use array_column()
eg:
$array = Array
(
[0] => Array
(
[id] => 21134
)
[1] => Array
(
[id] => 21135
)
[2] => Array
(
[id] => 21137
)
[3] => Array
(
[id] => 21138
)
[4] => Array
(
[id] => 21144
)
[5] => Array
(
[id] => 21147
)
[6] => Array
(
[id] => 21149
)
[7] => Array
(
[id] => 21150
)
[8] => Array
(
[id] => 21151
)
[9] => Array
(
[id] => 21152
)
[10] => Array
(
[id] => 21153
)
)
use array_column() to convert it, like this,
$result = array_column($array,'id');
You can use array_map for multidimensional array into one-dimensional array
$oneDimensionalArray = array_map('current', $twoDimensionalArray);
you can also array_merge
$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);
I have following array from google api and wanted to get id only. How do i get id's from following. when i try to get size it gives me size zero.
Google_Service_Drive_FileList Object ( [collection_key:protected] => items
[internal_gapi_mappings:protected] => Array ( ) [etag] => [itemsType:protected]
=> Google_Service_Drive_DriveFile [itemsDataType:protected] => array [kind] =>
[nextLink] => [nextPageToken] => [selfLink] => [modelData:protected] => Array (
[items] => Array ( [0] => Array ( [id] => 0B0OnHwH_cQckeWZPdXFyRU5aMGs ) [1] =>
Array ( [id] => 0B0OnHwH_cQckaUVORkZaM2NoRXM ) [2] => Array ( [id] =>
1kCQLhEgzgeKO-L57ISWjQL4ctkxT4Gq2wrdzDFbrcac ) [3] => Array ( [id] =>
0B0OnHwH_cQckc3RhcnRlcl9maWxl ) [4] => Array ( [id] => 1-Yhs92vZnvUNArwAcZJZ9xa-
fXZ7ZgRrADyF-ikG1gU ) ) ) [processed:protected] => Array ( ) ) Array ( )
if you mean the object attributes, you can do
$values = array();
$keys = array();
foreach($object as $key=>$value)
{
array_push($keys,$key);
array_push($values,$value);
}
This will give you each object attribute and its corresponding value
i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.
Here is my problem how can i get the array[1]
i have an foreach on my view i can't get the value of [1] => Array?
Array
(
[0] => Array
(
[details] => Array
(
[product_id] => 1
[0] => Array
(
[field_name] => 犬種
[field] => ペã‚ニーズ
)
[1] => Array
(
[field_name] => 生年月日
[field] => 2013年12月13日
)
[2] => Array
(
[field_name] => 性別
[field] => メス
)
[3] => Array
(
[field_name] => ä¾¡æ ¼
[field] => ¥98,000
)
)
[image] => Array
(
[0] => Array
(
[filename] => 1394462124.jpg
[0] => 1394462124.jpg
)
)
)
[1] => Array
(
[details] => Array
(
[product_id] => 2
[0] => Array
(
[field_name] => 犬種
[field] => ペã‚ニーズ
)
[1] => Array
(
[field_name] => 生年月日
[field] => 2013年12月13日
)
[2] => Array
(
[field_name] => 性別
[field] => オス
)
[3] => Array
(
[field_name] => ä¾¡æ ¼
[field] => ¥88,000
)
)
[image] => Array
(
[0] => Array
(
[filename] => 1394462181.jpg
[0] => 1394462181.jpg
)
)
)
i will fetch the [1] => Array inside the
i can't get the [1] => Array using foreach on my view what is can i get the value on array[1]
If you just want to loop through and deal with each dataset, the following code will do that:
foreach ($arr as $key => $data) {
// Output data
// The first row will be $arr[0], and the second $arr[1], etc…
}
-- Original answer
Assuming you mean the second array within the top-level array and that you only want to access the second element in that array (index 1), you should just be able to access it as follows:
$arr[1]
eg:
$second_photo = $arr[1];
Where $arr is the variable name you've assigned to the array you've var_dump()ed above.
That said, your question is ambiguous and not very clear. Can you please provide more information as to what you are actually trying to achieve? (Loop though and display all instances, for example?)
There has been a few other questions regarding replacing values in multidimensional array on here, but I didn't find anything regarding what I was trying to do, exactly, per se.
I have an array that I get from an API and I need to update a few values based on other values in the array tree before sending the API payload to the browser.
In the array when the sale[0] === true I am looking for some logic to then replace the [price][0] value with a corresponding new sale price.
Using the foreach, I can easily loop through the each of the nodes in the array, but I am unsure once I loop through the array, how I can update the original array with new price if/when sale node === true.
Array
(
[response] => Array
(
[0] => Array
(
[results] => Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[id] => Array
(
[0] => 846471605959
)
[title] => Array
(
[0] => Test Item 846471605959
)
[imageURL] => Array
(
[0] => https://foo/bar/images/846471605959.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/846471605959
)
[price] => Array
(
[0] => 799.00
)
[sale] => Array
(
[0] => true
)
)
[1] => Array
(
[id] => Array
(
[0] => 414953260545
)
[title] => Array
(
[0] => Test Item 414953260545
)
[imageURL] => Array
(
[0] => https://foo/bar/images/414953260545.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/414953260545
)
[price] => Array
(
[0] => 199.00
)
[sale] => Array
(
[0] => false
)
)
[2] => Array
(
[id] => Array
(
[0] => 684865199812
)
[title] => Array
(
[0] => Test Item 684865199812
)
[imageURL] => Array
(
[0] => https://foo/bar/images/684865199812.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/684865199812
)
[price] => Array
(
[0] => 699.00
)
[sale] => Array
(
[0] => false
)
)
[3] => Array
(
[id] => Array
(
[0] => 987800965761
)
[title] => Array
(
[0] => Test Item 987800965761
)
[imageURL] => Array
(
[0] => https://foo/bar/images/987800965761.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/987800965761
)
[price] => Array
(
[0] => 499.00
)
[sale] => Array
(
[0] => true
)
)
[4] => Array
(
[id] => Array
(
[0] => 005457536677
)
[title] => Array
(
[0] => Test Item 005457536677
)
[imageURL] => Array
(
[0] => https://foo/bar/images/005457536677.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/005457536677
)
[price] => Array
(
[0] => 99.00
)
[sale] => Array
(
[0] => false
)
)
)
)
)
}
)
)
Use a reference variable for the foreach iteration variable, then you can update the element in place.
foreach $data['response'][0]['results'][0]['items'] as &$item) {
if ($item['sale'][0]) {
$item['price'][0] = $new_price;
}
}
If you also need to loop through all the elements in the sale array, add a nested loop.
foreach $data['response'][0]['results'][0]['items'] as &$item) {
foreach ($item['sale'] as $i => $sale) {
if ($sale) {
$item['price'][$i] = $new_price;
}
}
}