How map array of array to single assoc array? - php

I have:
array:2 [
0 => array:1 [
"FNAME" => "nullable|string"
]
1 => array:1 [
"LNAME" => "nullable|string"
]
]
And I try to get:
array:1 [
"key" => "value"
]
I try map it, but has problem

<?php
$array = [
[
"FNAME" => "nullable|string",
],
[
"LNAME" => "nullable|string",
]
];
$newArray = [];
foreach ($array as $item) {
foreach ($item as $key => $value) {
$newArray[$key] = $value;
}
}
print_r($newArray);
Will output:
Array
(
[FNAME] => nullable|string
[LNAME] => nullable|string
)

Two simple ways:
print_r(array_merge(...$arr));
// if `...` is not available (php < 5.6), then:
print_r(call_user_func_array('array_merge', $arr))

Related

How I can explode all string fields in Collection using Laravel

I have an array with fields
[
"house" => "30|30|30",
"street" => "first|second|third",
...
]
I want to get array
[
[
"house" => "30",
"street" => "first",
...
],
[
"house" => "30",
"street" => "second",
...
],
[
"house" => "30",
"street" => "third",
...
]
]
I know how I can solve this using PHP and loop, but maybe this problem has more beautiful solution
use zip
$data = [
"house" => "30|30|30",
"street" => "first|second|third",
];
$house = collect(explode('|',$data['house']));
$street = collect(explode('|',$data['street']));
$out = $house->zip($street);
$out->toarray();
Here's something I managed to do with tinker.
$original = [
"house" => "30|30|30",
"street" => "first|second|third",
];
$new = []; // technically not needed. data_set will instantiate the variable if it doesn't exist.
foreach ($original as $field => $values) {
foreach (explode('|', $values) as $index => $value) {
data_set($new, "$index.$field", $value);
}
}
/* dump($new)
[
[
"house" => "30",
"street" => "first",
],
[
"house" => "30",
"street" => "second",
],
[
"house" => "30",
"street" => "third",
],
]
*/
I tried using collections, but the main problem is the original array's length is not equal to the resulting array's length, so map operations don't really work. I suppose you can still use each though.
$new = []; // Since $new is used inside a Closure, it must be declared.
collect([
"house" => "30|30|30",
"street" => "first|second|third",
...
])
->map(fn($i) => collect(explode('|', $i))
->each(function ($values, $field) use (&$new) {
$values->each(function ($value, $index) use ($field, &$new) {
data_set($new, "$index.$field", $value);
});
});
$array = ["house" => "30|30|30","street" => "first |second| third"];
foreach($array as $key=> $values){
$explodeval = explode('|',$values);
for($i=0; $i<count($explodeval); $i++){
$newarray[$i][$key]= $explodeval[$i];
}
}
output:
Array
(
[0] => Array
(
[house] => 30
[street] => first
)
[1] => Array
(
[house] => 30
[street] => second
)
[2] => Array
(
[house] => 30
[street] => third
)
)

How to navigate back up in a multidimensional array after finding the appropriate child?

I have an array that looks like this:
RecursiveArrayIterator {#605 ▼
+"xs:schema": array:2 [▼
"value" => array:1 [▼
"xs:element" => array:2 [▼
"value" => array:1 [▼
"xs:complexType" => array:2 [▼
"value" => array:2 [▼
"xs:sequence" => array:2 [▼
"value" => array:1 [▼
"xs:element" => array:3 [▼
0 => array:2 [▼
"value" => array:1 [▼
"xs:simpleType" => array:2 [▼
"value" => array:1 [▼
"xs:restriction" => array:2 [▼
"value" => array:1 [▼
"xs:maxLength" => array:1 [▼
"attributes" => array:1 [▼
"value" => "40"
]
]
]
"attributes" => array:1 [▶]
]
]
"attributes" => []
]
]
"attributes" => array:1 [▼
"name" => "title"
]
]
1 => array:2 [▶]
2 => array:2 [▶]
]
]
"attributes" => []
]
"xs:attribute" => array:2 [▶]
]
"attributes" => []
]
]
"attributes" => array:1 [▼
"name" => "book"
]
]
]
"attributes" => []
]
}
I need to access the xs:maxLength attribute, so in order to that that, I'm using the following method:
private function findRestrictions(array $haystack, $needle)
{
$iterator = new \RecursiveArrayIterator($haystack);
$recursive = new \RecursiveIteratorIterator(
$iterator,
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ($recursive as $key => $value)
{
if ($key === $needle)
{
return (int)$value['attributes']['value'];
}
}
}
$maxLength = findRestrictions($array, 'xs:maxLength');
So that gives me back 40, just like expected. Anyway, my issue is that I need to know to which element this limit belongs to, which is mentioned in xs:element[0]['attributes']['name'] and I'm uncertain on how to reach there to grab the information I need, based on the match for xs:maxLength.
Well I have programmed a pretty good solution I think, this time it is tested.
My sample array:
$array = [
"we" =>
["are" => [
"lorem" => [
"gone" => "away",
"my" => "friend"
],
"never" => "abcd",
"any" => [
"btc" => "abc",
"help" => [
"mqf" => "bmx"
]
]
]
],
"fancy" => [
"lorem" => [
"gone" => "away",
"my" => "friend"
],
"never" => "abcd",
"any" => [
"btc" => "abc",
"help" => [
"mqf" => "bmx",
"abc" => 13
]
]
],
"beer" => "bar",
"helpful" => [
"meta" => "column",
"gamma" => [
"lorem" => [
"gone" => "mad",
"my" => "drink"
],
"never" => "abcd",
"any" => [
"btc" => "abc",
"help" => [
"mqf" => "bmx",
"abc" => "alot"
]
]
]
],
"elements" => [
0 => 88,
1 => 99
]
];
My solution:
function array_find_value_return_parent($array,$needle,$parentkey) {
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $key => $value) {
if($value === $needle) {
for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
if($iterator->getSubIterator($i)->key() === $parentkey) {
return $iterator->getSubIterator($i)->current();
}
}
}
}
}
function array_find_key_return_value($array,$findkey) {
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $key => $value) {
if($findkey === $key) {
return $iterator->current();
}
}
}
My test:
$findvalue = "alot";
$findparentkey = "gamma";
$findreturnkey = "gone";
echo array_find_key_return_value(array_find_value_return_parent($array,$findvalue,$findparentkey),$findreturnkey);
Output: mad
For your case it means that you might do the following:
$findvalue = "40";
$findparentkey = "xs:element";
$findreturnkey = "name";
echo array_find_key_return_value(array_find_value_return_parent($array,$findvalue,$findparentkey),$findreturnkey);
Expected output: title
Right?
I do not know your original data structure, so I just converted your data to a PHP array. You can use $aks = new ArrayKeySearcher($data, 'xs:maxLength'); to find the key you want. And you can make the search more complex to satisfy your requirement.
However, if you are using something like XML, it is highly recommended to use XML-based solutions, like XPath query (eg: http://php.net/manual/en/domxpath.query.php, http://php.net/manual/en/simplexmlelement.xpath.php). These methods are easier to use, faster and more accurate.
<?php
$data = [
"xs:schema"=> [
"value" => [
"xs:element" => [
"value" => [
"xs:complexType" => [
"value" => [
"xs:sequence" => [
"value" => [
"xs:element" => [
0 => [
"value" => [
"xs:simpleType" => [
"value" => [
"xs:restriction" => [
"value" => [
"xs:maxLength" => [
"attributes" => [
"value" => "40"
]
]
],
"attributes" => []
]
],
"attributes" => []
]
],
"attributes" => [
"name" => "title"
]
],
1 => [],
2 => [],
]
],
"attributes" => []
],
"xs:attribute" => []
],
"attributes" => []
]
],
"attributes" => [
"name" => "book"
]
]
],
"attributes" => []
]
];
class ArrayKeySearcher
{
public $data;
public $path;
public $value;
public function __construct($data, $key)
{
$this->data = $data;
$this->findKeyPath($data, $key);
}
private function findKeyPath($data, $key)
{
foreach ($data as $k => $v) {
$this->path[] = $k;
if ($key === $k) {
$this->value = $v;
return;
}
$this->findKeyPath($v, $key);
if (!is_null($this->value))
return;
array_pop($this->path);
}
}
public function arrayReverseSearch($a, $k, $pos = null)
{
$count = count($a);
$i = ($pos === null) ? ($count - 1) : $pos;
for(; $i >= 0; $i--) {
if($a[$i] === $k)
return $i;
}
return $i;
}
public function getValueByPath($path)
{
$v = $this->data;
foreach($path as $k) {
if(isset($v[$k]))
$v = $v[$k];
}
return $v;
}
}
$aks = new ArrayKeySearcher($data, 'xs:maxLength');
echo 'path: ' . json_encode($aks->path) . PHP_EOL;
echo 'value: ' . json_encode($aks->value) . PHP_EOL;
$p = $aks->path;
$pos = $aks->arrayReverseSearch($p, 'xs:simpleType');
$pos = $aks->arrayReverseSearch($p, 'value', $pos);
$p = array_slice($p, 0, $pos);
$parent = $aks->getValueByPath($p);
echo 'parent path: ' . json_encode($p) . PHP_EOL;
echo 'parent attributes: ' . json_encode($parent['attributes']) . PHP_EOL;
Output:
path: ["xs:schema","value","xs:element","value","xs:complexType","value","xs:sequence","value","xs:element",0,"value","xs:simpleType","value","xs:restriction","value","xs:maxLength"]
value: {"attributes":{"value":"40"}}
parent path: ["xs:schema","value","xs:element","value","xs:complexType","value","xs:sequence","value","xs:element",0]
parent attributes: {"name":"title"}

How to flip elements with numeric key in php?

I've an array in php like bellow:
array:2 [
"element_a" => array:1 [
"string" => array:3 [
"min" => 5
"max" => 50
0 => "nullable" // this element need be flipped
]
]
"element_b" => array:1 [
0 => "string" // this need too
]
]
And I need flip elements with numeric key value.. I want get the following output:
array:2 [
"element_a" => array:1 [
"string" => array:3 [
"min" => 5
"max" => 50
"nullable" => true
]
]
"element_b" => array:1 [
"string" => true
]
]
Any ideas about how i can get it?
Thanks.
Edit
I tried this, but no results:
array_walk_recursive($array, function (&$value, &$key) {
if (is_numeric($key)) {
list($value, $key) = [$key, $value];
}
});
This solution will fix your array in place (by reference):
function fix( array &$array ) {
foreach( $array as $key => &$value ) {
if( is_array( $value ) ) {
fix( $value );
}
else if( is_numeric( $key ) ) {
$array[ $value ] = true;
unset( $array[ $key ] );
}
}
}
$yourArray = [
"element_a" => [
"string" => [
"min" => 5,
"max" => 50,
0 => "nullable" // this element need be flipped
]
],
"element_b" => [
0 => "string" // this need too
]
];
fix( $yourArray );
var_dump( $yourArray );

Laravel save multiple records

I've got an array within arrays and would like to add something to it.
$options = $request->options;
foreach ($options as $option) {
$option['poll_id'] = $this->id;
}
dd($options);
But for some reason it does not add to the array.
So I receive this:
array:1 [
0 => array:1 [
"name" => "testtest"
]
]
But I would expect this:
array:1 [
0 => array:1 [
"name" => "testtest",
"poll_id" => 1
]
]
You're not changing $options so foreach is destroying $option with each iteration. Try something like this instead:
$options = [];
foreach ($request->options as $key => $value) {
$options[$key]['poll_id'] = $this->id;
}
You should do it using the $key attribute on arrays
// Suppose your $request->options is like:
$options = [
0 => [
"name" => "testtest"
]
];
foreach ($options as $key => $option) {
$options[$key]['poll_id'] = 3; // Changing variable - $options here.
}
and it should work!
// $options would be like:
array:1 [▼
0 => array:2 [▼
"name" => "testtest"
"poll_id" => 3
]
]

PHP Merge Multidimensional Array become one

I've got this array
array:2 [
0 => array:2 [
"monday_open" => "10:00:00"
"monday_close" => "20:00:00"
]
1 => array:2 [
"tuesday_open" => "00:00:00"
"tuesday_close" => "00:00:00"
]
]
How can i combine them become:
array:4 [
"monday_open" => "10:00:00"
"monday_close" => "20:00:00"
"tuesday_open" => "00:00:00"
"tuesday_close" => "00:00:00"
]
I've tried using array_walk_recrusive but it doesn't return me with key name:
array_walk_recursive($array, function ($v) use (&$arrayFlat) {
$arrayFlat[] = $v;
});
I've tried this one too but got the same result as array_walk_recrusive:
iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0)
Result:
array:4 [
0 => "10:00:00"
1 => "20:00:00"
2 => "00:00:00"
3 => "00:00:00"
]
Is there any other way to keep the key value?
You can add key param to the closure of the array_walk_recursive like this
array_walk_recursive($array, function ($v, $k) use (&$arrayFlat) {
$arrayFlat[$k] = $v;
});
Try this
$source_array = array(array("monday_open"=>"10:00:00", "monday_close" => "20:00:00"), array("tuesday_open" => "00:00:00", "tuesday_close" => "00:00:00"));
$my_array = array();
foreach($source_array as $source){
foreach($source as $key=> $val){
$my_array[$key] = $val;
}
}
print_r($my_array);

Categories