PHP wrapping arrays with a parent array - php

I have a structure as below:
array (size=1)
0 =>
array (size=2)
0 => string
1 => string
array (size=2)
0 =>
array (size=2)
0 => string
1 => string
1 =>
array (size=2)
0 => string
1 => string
What I want to do is wrapping this arrays with a "parent" array like I show below:
array
0=> array (size=1)
0 =>
array (size=2)
0 => string
1 => string
1=> array (size=2)
0 =>
array (size=2)
0 => string
1 => string
1 =>
array (size=2)
0 => string
1 => string
How can I achieve this?
Thank you!
EDIT
$__server_description_path_data = explode('}', $data);
foreach ($__server_description_path_data as $value) {
$_server_description_path_data[] = trim($value, '{HEX');
}
foreach ($_server_description_path_data as $value) {
$server_description_path_data[] = explode(":", $value);
}
The snippet above gives me structure like 1st part (array) of the question. What I'd like to do is getting a structure as in my second part of my question.
EDIT2
array
0=> array
0=> array
0=> string
1=> string
array
0=> array
0=> array
0=> string

$new_big_array = array($old_array1, $old_array2);

$array1 = array(1,2,3);
$array2 = array(3,4,5);
$array3 = array($array1, $array2);

presuming your top code shows two var_dumps, i have called the arrays array1 & array2:
$parent=array($array1, $array2);
EDIT so var_dump($server_description_path_data); provides the top output?
if so how about:
$wrapper= array($server_description_path_data);
var_dump($wrapper);

Related

How transform a php array in a php array with no index?

I already saw old questions here on stackoverflow but i have no success.
i´m trying to use a foreach as this:
$arr = [];
$id = '10';
foreach($ext as $e){
if(!empty($e)){
$only_filled[] = array_merge($arr, ['img'+($pointer+1) => $id.'_'.$e]);
}
}
Result i got:
array (size=2)
0 =>
array (size=1)
'img1' => string '10_1.jpg' (length=8)
1 =>
array (size=1)
'img3' => string '10_3.jpg' (length=8)
Result i need:
array (size=2)
'img1' => string '10_1.jpg' (length=8),
'img3' => string '10_3.jpg' (length=8)`
You need an associative array and in your code, you are creating a simple array
Try code like this
foreach($ext as $e){
if(!empty($e)){
$only_filled['img'.($pointer+1)] = 'Your Value';
}
}

Convert array index to custom value?

I have an array $indexedarray
printr($indexedarray) gives something like this
array (size=3)
0 => string 'Homes' (length=5)
1 => string 'Apartments' (length=10)
2 => string 'Villas' (length=6)
I want to change this arrays index also same as value, like
array (size=3)
'Homes' => string 'Homes' (length=5)
'Apartments' => string 'Apartments' (length=10)
'Villas' => string 'Villas' (length=6)
is it posssible??
You can use array_combine:
$indexedarray= ['Homes', 'Apartments', 'Villas'];
print_r(array_combine($indexedarray, $indexedarray));
Gives:
Array
(
[Homes] => Homes
[Apartments] => Apartments
[Villas] => Villas
)
But be aware that your duplicate values will be dropped. Keys will be unique!
Try This :
$myArray = [
0 => 'Homes',
1 => 'Apartments',
2 => 'Villas' ];
$newArray = [];
foreach($myArray as $key => $value){
$newArray[$value] = $value;
}
var_dump($newArray);

Explode Funciton not working in PHP Shows error line expects parameter 2

here is $_POST['members'] and I want to explode it with |
[members] => Array
(
[0] => test.com|test Melissa
[1] => eboo#abcd.com.au|Buckley test
[2] => testtest#test.com.au|test Ashley
[3] => testset.com.au|Forno test
[4] => get.com.au|test Nathan
[5] =>set.com.au|Brown test
)
I am trying with follows php code
$get=explode('|',$_POST['members']);
echo '<pre>';
print_r($get);
try something like
foreach($_POST['members'] as $str){
$get[] = explode('|',$str);
}
print_r($get);
as $_POST['members'] is array, you need to use explode in foreach by accessing all array elements:
foreach($_POST['members'] as $members)
{
$get=explode('|',$members);
echo '<pre>'; print_r($get);
}
Loop Through the Data Array, store the result of your Explode in a new Array and perhaps you got what you want like so:
<?php
$arr = [
0 => "test.com|test Melissa",
1 => "eboo#abcd.com.au|Buckley test",
2 => "testtest#test.com.au|test Ashley",
3 => "testset.com.au|Forno test",
4 => "get.com.au|test Nathan",
5 => "set.com.au|Brown test"
];
$arrSubData = array();
foreach($arr as $pipeDividedString){
$arrSubData[] = explode('|', $pipeDividedString);
}
var_dump($arrSubData);
DUMPS
array (size=6)
0 =>
array (size=2)
0 => string 'test.com' (length=8)
1 => string 'test Melissa' (length=12)
1 =>
array (size=2)
0 => string 'eboo#abcd.com.au' (length=16)
1 => string 'Buckley test' (length=12)
2 =>
array (size=2)
0 => string 'testtest#test.com.au' (length=20)
1 => string 'test Ashley' (length=11)
3 =>
array (size=2)
0 => string 'testset.com.au' (length=14)
1 => string 'Forno test' (length=10)
4 =>
array (size=2)
0 => string 'get.com.au' (length=10)
1 => string 'test Nathan' (length=11)
5 =>
array (size=2)
0 => string 'set.com.au' (length=10)
1 => string 'Brown test' (length=10)
$_POST['members'] is an array and explode(); works on strings. You'll have to loop through the array and explode each value. Something like this :
$arr = array(
'test.com|test Melissa',
'eboo#abcd.com.au|Buckley test',
'testtest#test.com.au|test Ashley',
'testset.com.au|Forno test',
'get.com.au|test Nathan',
'set.com.au|Brown test'
);
$get = array();
foreach ($_POST['members'] as $member) {
$get[] = explode('|',$member);
}
echo '<pre>'; print_r($get);
In order to explode the values you need to loop trough $_POST['members'], for that you can use foreach().
The following, is an example that uses explode() and list() :
<?php
$members = !empty($_POST['members']) ? $_POST['members'] : die("post members is empty");
foreach($members as $member)
{
list($siteEmail, $name) = explode("|", $member);
echo "<pre> $siteEmail $name </pre>";
}
http://ideone.com/qdDmSe
You can use like this
foreach($_POST['members'] as $val){
$get = explode('|',$val);
}
echo "<pre>";print_r($get);

php Get all values from multidimensional associative array

how can i get all values from multidimensional associative array
I dont want to use print_r want to control my array put all the value in normal array with unique values
my array is look like this
array (size=10)
0 =>
array (size=3)
0 =>
array (size=1)
'Campaign' => string 'DEMO' (length=4)
1 =>
array (size=1)
'Campaign' => string 'Home_Sec' (length=8)
2 =>
array (size=1)
'Campaign' => string '' (length=0)
1 =>
array (size=0)
empty
2 =>
array (size=0)
empty
3 =>
array (size=1)
0 =>
array (size=1)
'Campaign' => string 'Back_Brace' (length=10)
4 =>
array (size=2)
0 =>
array (size=1)
'Campaign' => string 'Home_Sec' (length=8)
1 =>
array (size=1)
'Campaign' => string '' (length=0)
5 =>
array (size=1)
0 =>
array (size=1)
'Campaign' => string 'home_Sec_2' (length=10)
6 =>
array (size=1)
0 =>
array (size=1)
'Campaign' => string 'Burial_Ins' (length=10)
7 =>
array (size=0)
empty
8 =>
array (size=0)
empty
9 =>
array (size=0)
empty
I dont want to use print_r want to control my array put all the value in normal array with unique values
array_walk is an option, but here's another option if you want to try something a bit more coded by yourself, solving this problem recursively
This will flatten any n-max level array into a single array that contains all the values of all the sub arrays (including the initial array itself)
<?php
$array = array(
1 => array(1, 2, 3, 4 => array(
1, 2, 3, 4
)),
4, 5);
function recurse_values($array) {
if (is_array($array)) {
$output_array = array();
foreach ($array as $key=>$val) {
$primitive_output = recurse_values($val);
if (is_array($primitive_output)) {
$output_array = array_merge($output_array, $primitive_output);
}
else {
array_push($output_array, $primitive_output);
}
}
return $output_array;
}
else {
return $array;
}
}
print_r(recurse_values($array));
?>
If you need unique values, at the end you can add a array_unique to do this.
You can use array_walk
$array = array(...); //your values here
function output($item, $key) {
echo $key . ' =>' . $item;
}
array_walk($array, 'output');
Are you asking how you can "flatten" this multi-dimensional array into a one dimension? Possible solutions to similar problems... How to Flatten a Multidimensional Array?

How to map the two arrays with a duplicate value?

I have two arrays.
$array1 = ['id_e' =>[91707, 91708]];
$array2 = ['id_s' => [18, 57]];
If I want to insert or delete into the database, I want to make one-to-many mappings on these two arrays. And the result I expect it to be a new array as shown below.
Final Array:
array (size=4)
0 =>
array (size=2)
'id_e' => int 91707
'id_s' => int 18
1 =>
array (size=2)
'id_e' => int 91707
'id_s' => int 57
array (size=2)
2 =>
array (size=2)
'id_e' => int 91708
'id_s' => int 18
3 =>
array (size=2)
'id_e' => int 91708
'id_s' => int 57
I'm stuck after returning array1 and array2. I'm a beginner in php.
How do I do this?
Easiest way is:
$res = array();
foreach ($array1['id_e'] as $ide)
foreach ($array2['id_s'] as $ids)
$res[] = array('id_e' => $ide, 'id_s' => $ids);

Categories