Hi I Have An Indexed Array Like This I Have Been Trying To Figure Out How can i change it to multi dimensional after every 4 elements:
array(8) {
[0]=>
string(3) "yes"
[1]=>
string(11) "John DOE"
[2]=>
string(3) "116"
[3]=>
string(15) "John DOE.jpeg"
[4]=>
string(24) "No"
[5]=>
string(11) "John Snow"
[6]=>
string(3) "116"
[7]=>
string(15) "JohnSnow.jpeg"
}
And I Want to have Something Like This a multidimensional array If Possible
array([0]=>{
[0]=>'Yes',
[1]=>'John Doe',
[2]=>'116,
[3]=>'JohnDoe.jpeg'
},[1]=>{
[0]=>'No',
[1]=>'John Snow',
[2]=>'116,
[3]=>'JohnSnow.jpeg'
}
you could also use this if you prefer to use core coding:-
<?php
$arr=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17];
$i=0;
$arra1=[];
$j=0;
foreach ($arr as $value) {
$arr1[$j][$i]=$value;
$i++;
if($i==4){
$i=0;
$j++;
}
}
print_r($arr1);
Related
I'm trying to loop through a multidimensional array with foreach but sometimes there's 5 dimensions and sometimes there's 2, but I need to foreach every array. Here is an example:
array(16) {
["id"]=>
string(2) "1"
["name"]=>
string(1) "Bob"
["job"]=>
array(2) {
[0]=>
string(8) "software"
[1]=>
string(7) "plumber"
}
["kids"]=>
array(2) {
[1]=>
array(2) {
[0]=>
string(4) "Jane"
[1]=>
string(4) "girl"
}
[2]=>
array(2) {
[0]=>
string(3) "Sam"
[1]=>
string(4) "boy"
[2] => array(2) {
[0]=>
string(3) "123"
[1]=>
string(11) "Main Street"
}
}
}
}
you get the point.... but imagine if I had a dimension of 10 in the array. How can I dynamically loop through them and do trim() to each value in the whole array?
Here is what I have so far:
foreach ($array as $key => $value) {
$array[$key] = trim($value);
}
but I need it to go deeper into an array if there is an array and perform trim to all values in my $array.
I have two arrays, I would like to compare.
array1:
array(4) {
["123"]=>
array(5) {
["animal"]=>
string(2) "cat"
["name"]=>
string(4) "fred"
}
["345"]=>
array(5) {
["animal"]=>
string(3) "dog"
["name"]=>
string(4) "alan"
}
["order"]=>
string(2) "12"
}
array2:
array(4) {
["123"]=>
array(5) {
["animal"]=>
string(2) "cat"
["name"]=>
string(4) "fred"
}
["345"]=>
array(5) {
["animal"]=>
string(3) "fox"
["name"]=>
string(4) "tom"
}
["order"]=>
string(2) "12"
}
I compare them with array_diff:
$result = array_diff($array1, $array2);
But if I var_dump $result, I get the following output:
array(0) {
}
Does anyone have an idea why?
For associative arrays you should use array_diff_assoc. Also see the user contributed notes for how to do this recursively, if you need to.
With the help of sinaza I found out that no difference was displayed, because array_diff works different with multidimensional arrays.
Here is the code, that worked for me:
foreach ($array1 as $k1 => $v1) {
if (array_diff($array2[$k1], $array1[$k1])){
$result[$k1] = array_diff($array2[$k1], $array1[$k1]);
}
}
I have the following array:
array(5) {
[0]=> array(3) {
[0]=> string(10) "2013-09-18"
[1]=> string(75) "Ready For Retina HD: Create Pixel-Perfect Assets For Multiple Scale Factors"
[2]=> string(74) "ready-for-retina-hd-create-pixel-perfect-assets-for-multiple-scale-factors"
}
[1]=> array(3) {
[0]=> string(10) "2010-10-20"
[1]=> string(40) "Taking A Closer Look At Tech Conferences"
[2]=> string(40) "taking-a-closer-look-at-tech-conferences"
}
[2]=> array(3) {
[0]=> string(10) "2014-10-19"
[1]=> string(29) "Wayfinding For The Mobile Web"
[2]=> string(29) "wayfinding-for-the-mobile-web"
}
[3]=> array(3) {
[0]=> string(10) "2014-05-15"
[1]=> string(24) "Freebie: Icons Of Autumn"
[2]=> string(23) "freebie-icons-of-autumn"
}
[4]=> &array(1){
[0]=> string(0) ""
}
}
How would I go about assigning key names to each part of the inner array? E,g date, title, pathname.
I understand you can do something like this to create an array with certain keys, but how does this work with multiple nested arrays? And how can it be assigned after array creation?
$keys = array('Date', 'Title', 'Filepath');
Assuming $array1 is your main array (with 5 values).
foreach($array1 as $a)
{
if (len($a) == 3)
$array2[] = array("Date" => $a[0], "Title" => $a[1], "Filepath" => $a[2]);
}
$array1 = $array2;
Use a foreach.
$new = [];
foreach ($origArray as $inner) {
$new[] = [
"date" => $inner[0],
"title" => $inner[1],
"filepath" => $inner[2]
];
}
$origArray = $new;
This doesn't handle cases where the item does not conform to the "standard" (e.g item 4) but this should get you started.
This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed last month.
I'm chasing my tail trying to combine the results of two different queries to output in a template.
I'm trying to merge the corresponding sub-arrays in model_data and entry_data to get desired_result. I will then iterate over desired_result and print values into the template.
Any assistance is greatly appreciated.
model_data
array(2) {
[0]=>
array(2) {
["entry_id"]=> string(3) "192"
["field_id_49"]=> string(10) "Model Name"
}
[1]=>
array(2) {
["entry_id"]=> string(3) "193"
["field_id_49"]=> string(5) "MN123"
}
}
entry_data
array(2) {
[0]=>
array(2) {
["uri"]=> string(24) "/products/product-title/"
["title"]=> string(13) "Product Title"
}
[1]=>
array(2) {
["uri"]=> string(22) "/products/lorem-ipsum/"
["title"]=> string(11) "Lorem Ipsum"
}
}
desired_result
array(2) {
[0]=>
array(4) {
["entry_id"]=> string(3) "192"
["field_id_49"]=> string(10) "Model Name"
["uri"]=> string(24) "/products/product-title/"
["title"]=> string(13) "Product Title"
}
[1]=>
array(4) {
["entry_id"]=> string(3) "193"
["field_id_49"]=> string(5) "MN123"
["uri"]=> string(22) "/products/lorem-ipsum/"
["title"]=> string(11) "Lorem Ipsum"
}
}
foreach($model_data as $key => $value){
$result[$key] = array_merge($entry_data[$key], $model_data[$key]);
}
You can use array_replace_recursive function to merge these arrays.
Below is a example:
$desired_result = array_replace_recursive($model_data, $entry_data);
var_dump($desired_result);
i have the following array:
["addToCart"]=>
array(3) {
[1]=>
array(5) {
["aantal"]=>
int(1)
["film_id"]=>
string(1) "1"
["zaal_id"]=>
string(1) "1"
["dag"]=>
string(7) "maandag"
["seats"]=>
array(4) {
[0]=>
string(2) "67"
[1]=>
string(2) "68"
[2]=>
string(2) "69"
[3]=>
string(2) "70"
}
}
You can see that i have an array called "seats" inside the "addToCart" array.There are 4 items in the "seats" array.
what i would like to have is 4 separate arrays, they should all have the same content but each of them needs to have 1 value of "seats".
I'm not sure I got exactly what you're looking to do, but this would result in an array of arrays where each has only one seat:
$seatArrays = array();
foreach ($addToCart as $arr)
{
foreach ($arr["seats"] as $seat)
{
$seatArr = $arr; // Copy the original array
$seatArr["seats"] = $seat; // Replace the "seats" subarray with the current seat
$seatArrays[] = $seatArr;
}
}