PHP rebuild a big array - php

array (size=10)
'image' =>
array (size=3)
0 => string 'BlackLingerie(42).jpg' (length=21)
1 => string 'BlackLingerie(43).jpg' (length=21)
2 => string 'BlackLingerie(44).jpg' (length=21)
'text' =>
array (size=3)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
'author' =>
array (size=3)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
'date' =>
array (size=3)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
'verImage' =>
array (size=3)
0 => string 'upload' (length=6)
1 => string 'upload' (length=6)
2 => string 'upload' (length=6)
'imagePicsPath' =>
array (size=3)
0 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(42).jpg'/' (length=77)
1 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(43).jpg'/' (length=77)
2 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(44).jpg'/' (length=77)
'imageThumbPath' =>
array (size=3)
0 => string 'http://127.0.0.1/develop/mvc/public/images/thumbs/BlackLingerie(42).jpg'/' (length=79)
1 => string 'http://127.0.0.1/develop/mvc/public/images/thumbs/BlackLingerie(43).jpg'/' (length=79)
2 => string 'http://127.0.0.1/develop/mvc/public/images/thumbs/BlackLingerie(44).jpg'/' (length=79)
'imagePath' =>
array (size=3)
0 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(42).jpg'/' (length=77)
1 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(43).jpg'/' (length=77)
2 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(44).jpg'/' (length=77)
'imageID' =>
array (size=3)
0 => string '0' (length=1)
1 => string '1' (length=1)
2 => string '2' (length=1)
'submitUploadImages' => string 'Ladda upp bilder till databas' (length=29)
Want to rebuild this array to an more useful array. Like this
array
( [image0] (
'name' =>
'text' =>
'author' =>
'date' =>
'verImage' =>
'imagePicsPath' =>
'imageThumbPath' =>
'imagePath' =>
'imageID' =>
)
[image1] (
'name' =>
'text' =>
'author' =>
'date' =>
'verImage' =>
'imagePicsPath' =>
'imageThumbPath' =>
'imagePath' =>
'imageID' =>
)
And so on depending on how many pictures there is, the keys inside the image array holds the values for each image. Like name, path so on. The incoming array is a $_POST that holds multiple form input data. Need some help to crack this one guys. Need to iterate trough the $_POST array, get the contents and transform to a new array ?
I want unique image arrays that holds the image information before doing my stuff with the database =)

I haven't tested this but it should work:
$incomingArray = $_POST['array'];
$sortedArray = array();
for($i = 0; $i < count($incomingArray); $i++){
foreach($incomingArray as $key => $value){
$sortedArray["image".$i][$key] = $value[i];
}
}
Doing it this way means you don't have to write $sortedArray["image".$i]['NAME'] = $incomingArray['NAME'][$i] for each image value (name, test, author etc.).

Try
foreach( $array as $1st_level_key => $1st_level_value ) {
foreach ( $1st_level_value as $2nd_level_key => $2nd_level_value ) {
$new_array['image'.$2nd_level_key][$1st_level_key] = $2nd_level_value;
}
}

Short answer yes, pretty much like this:
for($i = 0; $i < count($YourArray); $i++)
{
$NewArray["image".$i]["name"] = $YourArray["name"][$i];
...
}

Related

PHP Arrays: How to add 'key & value' to an existing array

I do not know how to add a key and value to the existing array. My array goes like this. Initially I have tried adding using array_push() but it added not as I needed it.
I have given my output after I gave the 'var_dump'.
array (size=5)
0 =>
array (size=3)
'id' => int 7
'title' => string 'Pongal' (length=6)
'start' => string '2016-05-16' (length=10)
1 =>
array (size=3)
'id' => int 8
'title' => string 'big day' (length=7)
'start' => string '2016-05-04' (length=10)
2 =>
array (size=3)
'id' => int 9
'title' => string 'marriage day' (length=12)
'start' => string '2016-05-19' (length=10)
3 =>
array (size=3)
'id' => int 10
'title' => string 'Karthiks bday' (length=14)
'start' => string '2016-06-11' (length=10)
4 =>
array (size=3)
'id' => int 12
'title' => string 'Election date announced' (length=23)
'start' => string '2016-06-01' (length=10)
Now, I'd like to insert array('sample_key' => 'sample_value') after all the elements of each array.
How can I do it? This is I want the result to be like this:-
array (size=5)
0 =>
array (size=4)
'id' => int 7
'title' => string 'Pongal' (length=6)
'start' => string '2016-05-16' (length=10)
‘color’ => ‘red’
1 =>
array (size=4)
'id' => int 8
'title' => string 'big day' (length=7)
'start' => string '2016-05-04' (length=10)
‘color’ => ‘red’
2 =>
array (size=4)
'id' => int 9
'title' => string 'marriage day' (length=12)
'start' => string '2016-05-19' (length=10)
‘color’ => ‘red’
3 =>
array (size=4)
'id' => int 10
'title' => string 'Karthiks bday' (length=14)
'start' => string '2016-06-11' (length=10)
‘color’ => ‘red’
4 =>
array (size=4)
'id' => int 12
'title' => string 'Election date announced' (length=23)
'start' => string '2016-06-01' (length=10)
‘color’ => ‘red’
Note that I have added 'color' => 'red' to all the indexes
Just do this: Working demo
using the & you can change the main array, and just use $val['color'] = 'red' to add a new key , value pair in the array.
foreach($arr as $key => &$val){
$val['color'] = 'red';
}
Note that the 'write-back' feature of the ampersand persists even after the loop has finished: resetting $val to a new value will change the last element in $val, which is often unexpected. There are three ways around this class of bug:
Avoid write-back and just use the full array expression to write values inside the loop;
Don't re-use the $val variable in the same scope, even for another foreach() loop;
Use unset() on the $val variable to disconnect it from the array it will write back to.
foreach($arr as $key => $row){
$arr[$key]['color']="red";
}

how to use array_map/array_walk function for multidimensional array

I have the following multidimensional array:
array (size=2)
10 =>
array (size=7)
1 =>
array (size=4)
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)
2 =>
array (size=4)
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
3 =>
array (size=4)
0 => string '10view3' (length=7)
1 => string '10ins3' (length=6)
2 => string '10edit3' (length=7)
3 => string '10del3' (length=6)
11 =>
array (size=7)
1 =>
array (size=4)
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
2 =>
array (size=4)
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
3 =>
array (size=4)
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
I want to convert it to the array like:
1 => array(10 => array(
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
),
2 => array(
10 => array(
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
),
11 => array(
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
)
),
3 => array(
10 => array(
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
)
please help me i confuse to solve this problem thanks in advance.
It's quite obvious for me how to implement this using foreach loop, but I wonder if it's possible to do this with PHP array functions like array_map or array_walk. Please use PHP 5.3 for the callback function. Thank you!
Using array map you can achieve like this
<?php
function merge_array($n, $m)
{
$data = array();
$data[10] = $n;
$data[11] = $m;
return $data;
}
$c = array_map("merge_array", $array[10], $array[11]);
print_r($c);
Using for loop it is very simple you can achieve like this
$count_10 = count($array[10]);
$count_11 = count($array[11]);
$count = ($count_10>$count_11)?$count_10:$count_11;
$data = array();
for($i=0; $i<$count; $i++)
{
if(isset($array[10][$i]))
{
$data[$i][] = $array[10][$i];
}
if(isset($array[11][$i]))
{
$data[$i][] = $array[11][$i];
}
}

Tri-mensional array looping

I'm really stuck at a certain point in my project. I'm trying to convert a two-dimensional array into a three-dimensional array. Currently I´m at this point:
array (size=4)
0 =>
array (size=4)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
1 =>
array (size=4)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
2 =>
array (size=4)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
3 =>
array (size=4)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
What I would like to do is make a new array with key values of array[ ][3] (I hope this makes any sense) so something like this:
array (size=2)
0 => array (size=2)
0 => array (size=2)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => array (size=2)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
Right now this is the best way I can explain it, I'm not sure if I make any sense or if this is even possible, but if anyone could shed a light on this it would be much appreciated!
Thanks in advance,
Thomas
$array = array();
$array[] = array('foo','foo','foo',1);
$array[] = array('a','a','a',1);
$array[] = array('b','a','a',0);
$array[] = array('c','a','a',0);
$new_array =group_by_sub_array_key($array,3);
print_r($new_array);
function group_by_sub_array_key($array,$kn){
$new_array = array();
foreach($array AS $v){
$key=$v[$kn];
if(!array_key_exists($v[$kn],$new_array)){
$new_array[$key]=array();
}
unset($v[$kn]);
$new_array[$key][] = $v;
}
return $new_array;
}

Trying to access an element of an embedded array

This is a large data structure with many embedded arrays. Each array has n elements, in this example, 37. I am trying to access the 'content' element in the 1st and 3rd position, of each of the arrays.
array[1]->percentage->content
and
array[3]->grade->content
$tdata is the variable that contains the data structure. I have tried looping over like this:
$i=0;
foreach($tdata as $td){
if($td[$i] == 1 OR $td[$i] == 3){
var_dump($td[$i])
}
$i++;
}
This is the data structure.
array (size=37)
0 =>
array (size=2)
'leader' =>
array (size=2)
'class' => string '...' (length=18)
'rowspan' => int 37
'itemname' =>
array (size=2)
'colspan' => int 6
'content' => string '...' (length=159)
1 =>
array (size=2)
'grade' =>
array (size=2)
'class' => string '...' (length=25)
'content' => string '43.37 %' (length=7)
'percentage' =>
array (size=2)
'class' => string ' oddd1 baggb itemcenter ' (length=25)
'content' => string '43.37 %' (length=7)
2 =>
array (size=1)
'leader' =>
array (size=2)
'class' => string ' oddd2 b1t b2b b1l' (length=18)
'rowspan' => int 32
3 =>
array (size=1)
'grade' =>
array (size=2)
'class' => string ' oddd2 baggb itemcenter ' (length=25)
'content' => string '6.30 %' (length=6)
.
.
.
array (size=37)
0 =>
array (size=2)
'leader' =>
array (size=2)
'class' => string '...' (length=18)
'rowspan' => int 37
'itemname' =>
array (size=2)
'colspan' => int 6
'content' => string '...' (length=159)
1 =>
array (size=2)
'grade' =>
array (size=2)
'class' => string '...' (length=25)
'content' => string '26.49 %' (length=7)
'percentage' =>
array (size=2)
'class' => string ' oddd1 baggb itemcenter ' (length=25)
'content' => string '26.49 %' (length=7)
2 =>
array (size=1)
'leader' =>
array (size=2)
'class' => string ' oddd2 b1t b2b b1l' (length=18)
'rowspan' => int 32
3 =>
array (size=1)
'grade' =>
array (size=2)
'class' => string ' oddd2 baggb itemcenter ' (length=25)
'content' => string '11.12 %' (length=6)
.
.
.
foreach($tdata as $i => $td)
{
if($i == 1 || $i == 3)
{
var_dump($td[$i]);
}
}
foreach($tdata as $key => $data ){
if( $key === 1 || $key === 3){
var_dump( $data );
}
}
$i=0;
foreach($tdata as $td){
if($i == 0 || $i == 2){
var_dump($td[$i])
}
$i++;
}

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