I'm trying to add arrays to an associative array in PHP. I know this isn't how you're supposed to use the keys but I'm parsing the array to XML which needs te same <line> tag.
Desired array:
array(
'line' => array(
// Ean-artikelcode
'Article_Eancode' => 8710624618216,
// Leveranciersartikelcode
'Article_Supplier_Partno' => 22304
),
'line' => array(
'Article_Eancode' => 8710622648216,
'Article_Supplier_Partno' => 22304
)
);
Which I am trying to get with this code:
$artikelenFormatted = array();
$artikelen = array(
'a',
'b',
'c'
);
foreach ($artikelen as $art) {
$artikelenFormatted['line'] = array(
"Article_Eancode" => "a",
"Article_Supplier_Partno" => "b"
);
}
Which produces:
array (size=1)
'line' =>
array (size=2)
'Article_Eancode' => string 'a' (length=1)
'Article_Supplier_Partno' => string 'b' (length=1)
Because $array['line'] keeps getting overwritten so there aren't multiple entries
How would I do this?
EDIT: Sample of the desired XML
<Lines>
<Line>
<Article_Eancode>87XXXXXXXXXXX</Article_Eancode>
<Article_Supplier_Partno>22304</Article_Supplier_Partno>
</Line>
<Line>
<Article_Eancode>87XXXXXXXXXXX</Article_Eancode>
<Article_Supplier_Partno>22303</Article_Supplier_Partno>
</Line>
<Line>
<Article_Eancode>87XXXXXXXXXXX</Article_Eancode>
<Article_Supplier_Partno>22324</Article_Supplier_Partno>
</Line>
<Line>
<Article_Eancode>87XXXXXXXXXXX</Article_Eancode>
<Article_Supplier_Partno>22305</Article_Supplier_Partno>
</Line>
<Line>
<Article_Eancode>87XXXXXXXXXXX</Article_Eancode>
<Article_Supplier_Partno>22323</Article_Supplier_Partno>
</Line>
</Lines>
An array cannot have two (or more) of the same key. Consider; what would $array['line'] return?
What you're looking for is:
foreach ($artikelen as $art) {
$artikelenFormatted['line'][] = array(
"Article_Eancode" => "a",
"Article_Supplier_Partno" => "b"
);
}
Notice the [] after ['line']. This will make $artikelenFormatted['line'] an array where each element is an array of the data.
Edit:
To get it to work with XML, use the following:
foreach ($artikelen as $art) {
$artikelenFormatted[]['line'] = array(
"Article_Eancode" => "a",
"Article_Supplier_Partno" => "b"
);
}
And amend the array_to_xml function you reference to:
function new_array_to_xml( $data, &$xml_data ) {
foreach( $data as $key => $value ) {
if( is_array($value) ) {
if( is_numeric($key) ){
array_to_xml($value, $xml_data);
}
else
{
$subnode = $xml_data->addChild($key);
array_to_xml($value, $subnode);
}
} else {
$xml_data->addChild("$key",htmlspecialchars("$value"));
}
}
}
I solved this by using:
foreach ( $artikelen as $art ) {
$artikelenFormatted [] = array (
"Article_Eancode" => "a",
"Article_Supplier_Partno" => "b"
);
Which would output
array (size=2)
0 =>
array (size=2)
'Article_Eancode' => string 'a' (length=1)
'Article_Supplier_Partno' => string 'b' (length=1)
1 =>
array (size=2)
'Article_Eancode' => string 'a' (length=1)
'Article_Supplier_Partno' => string 'b' (length=1)
And editing my array_to_xml() function to change numeric keys to the string 'line' which produces the right XML
private function array_to_xml($entries, &$tmpXML) {
foreach ( $entries as $key => $value ) {
if (is_array ( $value )) {
if (! is_numeric ( $key )) {
$subnode = $tmpXML->addChild ( "$key" );
$this->array_to_xml ( $value, $subnode );
} else {
$subnode = $tmpXML->addChild ( "line" );
$this->array_to_xml ( $value, $subnode );
}
} else {
$tmpXML->addChild ( "$key", htmlspecialchars ( "$value" ) );
}
}
}
Thanks for everyone's input
You can't have the same keys but you can have :
EDIT 1 :
array(
[0] => array(
'line' => array(
// Ean-artikelcode
'Article_Eancode' => 8710624618216,
// Leveranciersartikelcode
'Article_Supplier_Partno' => 22304
)),
[1] => array(
'line' => array(
'Article_Eancode' => 8710622648216,
'Article_Supplier_Partno' => 22304
))
);
And you can do it like this :
foreach ($artikelen as $art) {
$artikelenFormatted[] = array(
'line' => array(
"Article_Eancode" => "a",
"Article_Supplier_Partno" => "b"
);
}
Related
I have a multidimensional array of undefined depth.
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo",
"something" => 42,
"something2" => [1,2,3]
)
)
);
I need to parse through it, find all the values that are plane text and save them in another array keeping the pathway. So I expect the final array be like this:
$array = array(
"foo" => "bar",
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
At the moment I'm trying to use recurrent function
$this->printAll($array);
public function printAll($a)
{
if (!is_array($a)) {
echo $a, ' <br>'; // here we can check if it is string and add to the final array
return;
}
foreach($a as $i=>$v) {
$this->printAll($v);
echo $i;
}
}
Could someone help me to figure out how to keep indexes through iterations and put it in the final array.
<?php
function printAll($array, &$save)
{
foreach ($array as $key => $values)
{
if ( ! is_numeric($values))
{
if (is_array($values))
{
printAll($values, $save[$key]);
}
else
{
$save[$key] = $values;
}
}
}
if ( ! empty($save)) {
$save = array_filter($save);
}
}
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo",
"something" => 42,
"something2" => [1,2,3]
)
)
);
$save = array();
printAll($array, $save);
print_r($save);
Outputs: PHP
Array (
[foo] => bar
[multi] => Array (
[dimensional] => Array (
[array] => foo
)))
I have two arrays :
groups = array (
array (1 => string 'INFORMATIQUE ET MULTIMEDIA'),
array (2 => string 'VEHICULES' ),
array (3 => string 'IMMOBILIER' ),
array (4 => string 'POUR LA MAISON ET JARDIN'),
array (5 => string 'HABILLEMENT ET BIEN ETRE'),
array (6 => string 'LOISIRS ET DIVERTISSEMENT'),
array (7 => string 'EMPLOI ET SERVICE' ),
array (8 => string 'ENTREPRISE' ),
array (9 => string 'AUTRES' ));
This is an array of categories groups
I have in the other side an array of categories :
$categories = array (
array (
'id' => string '1' ,
'name' => string 'Téléphones' ,
'groupid' => string '1'
),
array (
'id' => string '2',
'name' => string 'Tablette' ,
'groupid' => string '1'
),
array (
'id' => string '3' ,
'name' => string 'Voitures' ,
'groupid' => string '2'
),
array (
'id' => string '4' ,
'name' => string 'Motos',
'groupid' => string '2'
)
);
What i want is :
$result = array (
'INFORMATIQUE ET MULTIMEDIA' =>
array (
1 => string 'Téléphones',
2 => string 'Tablette'
)
'VEHICULES' =>
array (
4 => string 'Motos',
4 => string 'Motos'
)
);
This is my code but it doesn't work but the problem is that it records a single line :
foreach($groups as $id => $name)
{
$n = 1;
foreach($categories as $k=>$v)
{
if($v['groupid'] == $id){
$result[$name] = array_fill($v['id'], 1, $v['name']);
$n ++;
}
}
}
try this code, it will work for you
<?php
$result = null;
foreach($goups as $key => $value)
foreach($categories as $categorie)
if( $key == $categorie['groupid'] )
$result[$value][] = $categorie['name']
?>
foreach($groups as $id => $name)
{
foreach($categories as $k=>$v)
{
if($v['groupid'] == $id){
$result[$name][] = array($v['id'] => $v['name']);
}
}
}
or a simple one.
foreach($categories as $k=>$v)
{
$result[$groups[$v['groupid']]][] = array($v['id'] => $v['name']);
}
You got most of the code right, but you keep assigning all your data in the first index of your sub-array, which is why you get one single result. Try the code below
foreach($groups as $id => $name)
{
$n = 1;
foreach($categories as $k=>$v)
{
if($v['groupid'] == $id){
$result[$name][$n] = $['name'];
$n++;
}
}
}
I want to compare a value of data to a list of element which I had retrieved from php array(decoded json).
First,
This is the first array:
Array1
(
[0] => Array
(
[member_id] => 3
[member_card_num] => 2013011192330791
[member_barcode] => 2300067628912
)
[1] => Array
(
[member_id] => 4
[member_card_num] => 2328482492740000
[member_barcode] => 3545637000
)
[2] => Array
(
[member_id] => 2
[member_card_num] => 40001974318
[member_barcode] => 486126
)
[3] => Array
(
[member_id] => 1
[member_card_num] => 91001310000057698
[member_barcode] => 000057698
)
)
This is the second Array:
Array2
(
[0] => Array
(
[member_id] => 2
[member_card_num] => 40001974318
[member_barcode] => 486126
)
)
Second,
I had retrieved the (member_barcode) which I required.
Here is the code:
For Array1:
foreach ($decode1 as $d){
$merchant_barcode = $d ['member_barcode'];
echo $merchant_barcode;
}
For Array2:
foreach ($decode2 as $d2){
$user_barcode = $d2 ['member_barcode'];
echo $user_barcode;
}
Then,
I get this output():
For Array1(merchant_barcode):
2300067628912
3545637000
486126
000057698
For Array2(user_barcode):
486126
The question is, I would to check and compare whether the user_barcode in Array2(486126) is exist/match to one of the merchant_barcode in Array1.
This is my code,
but it only compare the user_barcode in Array2 to the last element(000057698) in Array1,
I want it to loop through each n check one by one. How can I do that?
public function actionCompareBarcode($user_barcode, $merchant_barcode){
if(($user_barcode) == ($merchant_barcode)){
echo "barcode exist. ";
}
else{
echo "barcode not exist";
}
}
In this case, the output I get is "barcode not exist", but it should be "barcode exist".
Anyone can help? Appreciate that. Im kinda new to php.
You could use a nested loop like:
foreach ($decode2 as $d2)
{
$user_barcode = $d2 ['member_barcode'];
foreach ($decode1 as $d)
{
$merchant_barcode = $d ['member_barcode'];
if ($merchant_barcode == $user_barcode)
{
echo "Match found!";
}
else
{
echo "No match found!";
}
}
}
<?
$a = array(
array(
'member_id' => 3,
'member_card_num' => '2013011192330791',
'member_barcode' => '2300067628912',
),
array(
'member_id' => 4,
'member_card_num' => '2328482492740000',
'member_barcode' => '3545637000',
),
array(
'member_id' => 2,
'member_card_num' => '40001974318',
'member_barcode' => '486126',
),
array(
'member_id' => 1,
'member_card_num' => '91001310000057698',
'member_barcode' => '000057698',
)
);
$b = array(
array(
'member_id' => 2,
'member_card_num' => '40001974318',
'member_barcode' => '486126',
)
);
array_walk($a, function($item) use($b) {
echo ($b['0']['member_barcode'] == $item['member_barcode'] ? "found" : NULL);
});
?>
I'd use array_uintersect() to calculate if those multidimensional arrays have a common element:
<?php
$a = array(
array(
'member_id' => '3',
'member_card_num' => '2013011192330791',
'member_barcode' => '2300067628912',
),
array(
'member_id' => '2',
'member_card_num' => '40001974318',
'member_barcode' => '486126',
)
);
$b = array(
array(
'member_id' => '2',
'member_card_num' => '40001974318',
'member_barcode' => '486126',
)
);
$match = array_uintersect($a, $b, function($valueA, $valueB) {
return strcasecmp($valueA['member_barcode'], $valueB['member_barcode']);
});
print_r($match);
Try calling that method as you are looping through Array1 and comparing the user_barcode to every value
You can compare two array this way too:
$per_arr = array();
$permissions = array()
foreach ($per_arr as $key => $perms) {
if(isset($permissions[$key]['name'])){
echo $per_arr[$key]['name']; //matched data
}else{
echo $per_arr[$key]['name']; //not matched data
}
}
I've here two multi-dimensional arrays.
How would you do to get the image_to_get value in the $b array thanks to the $a array ?
$a = array(
'thumbs' => array(
'0' => array(
'thumb1a' => array(
'0' => array(
'thumb1' => ""
)
)
)
)
);
$b = array(
'thumbs' => array(
'0' => array(
'thumb1a' => array(
'0' => array(
'thumb1' => "image_to_get"
)
),
'thumb2' => 'image2',
'thumb3' => 'image3',
'thumb4' => 'image4',
'thumb5' => 'image5',
)
)
);
You can try with:
function getAPath($array) {
if (empty($array)) {
return array();
}
$key = key($array);
return array_merge(array($key), getAPath($array[$key]));
}
function getBValue($array, $path) {
$key = array_shift($path);
if (is_null($key) || empty($array)) {
return $array;
}
return getBValue($array[$key], $path);
}
$aPath = getAPath($a);
$bValue = getBValue($b, $aPath);
var_dump($bValue);
First function getAPath flatterns your $a array into:
array (size=5)
0 => string 'thumbs' (length=6)
1 => int 0
2 => string 'thumb1a' (length=7)
3 => int 0
4 => string 'thumb1' (length=6)
Second function getBValue walks through the $b array using $aPath.
Below lovely one-liners ;-)
function getAPath($array) {
return empty($array) ? array() : array_merge(array($key = key($array)), getAPath($array[$key]));
}
function getBValue($array, $path) {
return (is_null($key = array_shift($path)) || empty($array)) ? $array : getBValue($array[$key], $path);
}
I've been breaking my head over this one but can't seem to find a solution. I need a function that retrieves all parent keys of a given child key. So for example if I have an array like this:
array(
'apples' => array(
'bananas' => array(
'strawberries' => array(
'fruit' => array()
)
)
)
)
I would call the function like 'key_get_parents($key, $array)', and it would return an array with all the parent keys. In this example that would be array('apples', 'bananas', 'strawberries').
$array = array(
'apples' => array(
'bananas' => array(
'strawberries' => array(
'fruit' => array()
)
)
)
);
function key_get_parents($subject, $array)
{
foreach ($array as $key => $value)
{
if (is_array($value))
{
if (in_array($subject, array_keys($value)))
return array($key);
else
{
$chain = key_get_parents($subject, $value);
if (!is_null($chain))
return array_merge(array($key), $chain);
}
}
}
return null;
}
// Prints "Array ( [0] => apples [1] => bananas )"
print_r(key_get_parents('strawberries', $array));