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

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";
}

Related

Loading docx (xml) into php array not working as expected. I want to keep original order

I'm trying to load a word (docx) document from PHP and it "kind of works"...
This code would produce an array from the document.xml part of the word-document:
$result = file_get_contents( 'zip://test.docx#word/document.xml' );
$content_arr = simplexml_load_string($result,null, 0, 'w', true);
BUT the nodes would rearranged when getting into the array with simplexml_load_string (and I don't want this rearrangement), where each node-type seems to be sorted out:
from:
p: some text
tbl: table
p: some text
p: some text
p: some text
to:
p: some text
p: some text
p: some text
p: some text
tbl: table
I would like something like this (that actually reflects the original order of the xml-nodes):
item0: p: some text
item1: tbl: table
item2: p: some text
item3: p: some text
item4: p: some text
Is this possible or is simplexml_load_string simply wrong function to use for my purpose?
It is. SimpleXML does not work well with mixed child nodes (text and element siblings). So it does not work with complex XML formats like OOXML.
Reading data from OOXML with DOM+Xpath is actually quite easy:
$xml = <<<"XML"
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>one</w:p>
<w:p>two</w:p>
<w:tbl>three</w:tbl>
<w:p>four</w:p>
</w:body>
</w:document>
XML;
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
// register a prefix for the used namespace
$xpath->registerNamespace(
'word', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
);
// iterate any p and tbl element nodes
foreach ($xpath->evaluate('//word:p|//word:tbl') as $index => $node) {
echo "item{$index} {$node->localName}: {$node->textContent}\n";
}
Output:
item0 p: one
item1 p: two
item2 tbl: three
item3 p: four
I found a function in php that would really make my day (and I hope it will make yours if you have the same issue that I had). Actually I think it would be much easier to implement now (and debug) because the way the xml is converted into the array(s).
The function is xml_parse_into_struct: https://www.php.net/manual/en/function.xml-parse-into-struct.php
$result = file_get_contents( 'zip://' . $file . '#word/document.xml' );
$p = xml_parser_create();
xml_parse_into_struct($p, $result, $content_arr, $index);
xml_parser_free($p);
var_dump($content_arr);
The dump looks like:
array (size=1087)
0 =>
array (size=4)
'tag' => string 'W:DOCUMENT' (length=10)
'type' => string 'open' (length=4)
'level' => int 1
'attributes' =>
array (size=20)
'XMLNS:WPC' => string '{value}' (length=66)
'XMLNS:CX' => string '{value}' (length=66)
'XMLNS:CX1' => string '{value}' (length=60)
'XMLNS:MC' => string '{value}' (length=59)
'XMLNS:O' => string '{value}' (length=39)
'XMLNS:R' => string '{value}' (length=67)
'XMLNS:M' => string '{value}' (length=58)
'XMLNS:V' => string '{value}' (length=29)
'XMLNS:WP14' => string '{value}' (length=67)
'XMLNS:WP' => string '{value}' (length=70)
'XMLNS:W10' => string '{value}' (length=37)
'XMLNS:W' => string '{value}' (length=60)
'XMLNS:W14' => string '{value}' (length=52)
'XMLNS:W15' => string '{value}' (length=52
'XMLNS:W16SE' => string '{value}' (length=58)
'XMLNS:WPG' => string '{value}' (length=65)
'XMLNS:WPI' => string '{value}' (length=63)
'XMLNS:WNE' => string '{value}' (length=52)
'XMLNS:WPS' => string '{value}' (length=65)
'MC:IGNORABLE' => string '{value}' (length=18)
1 =>
array (size=3)
'tag' => string 'W:BODY' (length=6)
'type' => string 'open' (length=4)
'level' => int 2
2 =>
array (size=4)
'tag' => string 'W:P' (length=3)
'type' => string 'open' (length=4)
'level' => int 3
'attributes' =>
array (size=3)
'W:RSIDR' => string '00DB5D45' (length=8)
'W:RSIDRDEFAULT' => string '00DB5D45' (length=8)
'W:RSIDP' => string '00DB5D45' (length=8)
3 =>
array (size=3)
'tag' => string 'W:PPR' (length=5)
'type' => string 'open' (length=4)
'level' => int 4
4 =>
array (size=4)
'tag' => string 'W:PSTYLE' (length=8)
'type' => string 'complete' (length=8)
'level' => int 5
'attributes' =>
array (size=1)
'W:VAL' => string 'Rubrik1' (length=7)
5 =>
array (size=4)
'tag' => string 'W:JC' (length=4)
'type' => string 'complete' (length=8)
'level' => int 5
'attributes' =>
array (size=1)
'W:VAL' => string 'center' (length=6)
6 =>
array (size=3)
'tag' => string 'W:PPR' (length=5)
'type' => string 'close' (length=5)
'level' => int 4
7 =>
array (size=3)
'tag' => string 'W:R' (length=3)
'type' => string 'open' (length=4)
'level' => int 4
8 =>
array (size=5)
'tag' => string 'W:T' (length=3)
'type' => string 'complete' (length=8)
'level' => int 5
'attributes' =>
array (size=1)
'XML:SPACE' => string 'preserve' (length=8)
'value' => string 'Skövde Konståkningsklubb inbjuder till ' (length=41)
9 =>
array (size=3)
'tag' => string 'W:R' (length=3)
'type' => string 'close' (length=5)
'level' => int 4
10 =>
array (size=3)
'tag' => string 'W:R' (length=3)
'type' => string 'open' (length=4)
'level' => int 4
11 =>
array (size=3)
'tag' => string 'W:BR' (length=4)
'type' => string 'complete' (length=8)
'level' => int 5
12 =>
array (size=4)
'tag' => string 'W:T' (length=3)
'type' => string 'complete' (length=8)
'level' => int 5
'value' => string 'dagläger i konståkning' (length=24)
13 =>
array (size=3)
'tag' => string 'W:R' (length=3)
'type' => string 'close' (length=5)
'level' => int 4
14 =>
array (size=3)
'tag' => string 'W:P' (length=3)
'type' => string 'close' (length=5)
'level' => int 3
15 =>
array (size=4)
'tag' => string 'W:P' (length=3)
'type' => string 'open' (length=4)
'level' => int 3
'attributes' =>
array (size=3)
'W:RSIDR' => string '00DB5D45' (length=8)
'W:RSIDRDEFAULT' => string '00A14053' (length=8)
'W:RSIDP' => string '00DB5D45' (length=8)

PHP. array_column() analogue for SimpleXMLElement object

object(SimpleXMLElement)[803]
public 'row' =>
array (size=13)
0 =>
object(SimpleXMLElement)[797]
public '#attributes' =>
array (size=4)
'codeonimage' => string '01' (length=2)
'name' => string 'Крышка' (length=12)
'oem' => string '13711251885' (length=11)
'ssd' => string '$HgsQRnNPF0d$' (length=174)
public 'attribute' =>
array (size=2)
0 =>
object(SimpleXMLElement)[813]
public '#attributes' =>
array (size=3)
'key' => string 'amount' (length=6)
'name' => string 'Количество' (length=20)
'value' => string '1' (length=1)
1 =>
object(SimpleXMLElement)[814]
public '#attributes' =>
array (size=3)
'key' => string 'end_of_production' (length=17)
'name' => string 'end_of_production' (length=17)
'value' => string 'Не производтся с: 19871116' (length=40)
...
I'm trying to take all the 'oem' values from each SimpleXML element like:
array_column($simpleXMLObject->row, 'oem');
... and of course I get an error:
array_column() expects parameter 1 to be array, object given
There is another option with a full search. But maybe there is some more pretty way to do this?

Argument 2 passed to array_sort() must be callable, string given

I have the following arrays:
array (size=3)
0 =>
array (size=3)
'id' => int 18
'class' => string 'VIP' (length=3)
'fee' => float 20
1 =>
array (size=3)
'id' => int 19
'class' => string 'VVIP' (length=4)
'fee' => float 50
2 =>
array (size=3)
'id' => int 20
'class' => string 'STANDARD' (length=8)
'fee' => float 5
array (size=3)
0 =>
array (size=3)
'id' => int 19
'class' => string 'VVIP' (length=4)
'fee' => int 50
1 =>
array (size=3)
'id' => int 18
'class' => string 'VIP' (length=3)
'fee' => int 20
2 =>
array (size=3)
'id' => int 20
'class' => string 'STANDARD' (length=8)
'fee' => int 5
Now i am trying to sort them both using array_sort in ascending order using the id.:
$array_1 = array_sort($array_1, 'id', SORT_ASC);
$array_2 = array_sort($array_2, 'id', SORT_ASC);
However i keep getting the following error:
Argument 2 passed to array_sort() must be callable, string given
The second parameter of array_sort should be a closure, not a string:
$array_1 = array_sort( $array_1, function($value){
return $value['id'];
});

PHP rebuild a big array

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];
...
}

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