how to merge them into one array in laravel - arrays

$category = Category::with('products')->where('seller_id','=',$request->
user('seller')->id)->get();
foreach($category as $index=>$opt){
foreach($opt->products as $key => $value)
{
print_r( array_column([$key => $value],'id'));
}
}
Array ( [0] => 33 ) Array ( [0] => 32 ) Array ( [0] => 36 ) Array ( [0] => 34 ) Array ( [0] => 35 )
i want this as my output
Array ( [0] => 33 ,[0] => 32 ,[0] => 36 , [0] => 34 , [0] => 35 )

Related

rearrange php multidimensional array according to id

-I cannot figure out how to regroup this array accourding to ids.
This array comes from a mysql table. the number 17 and 20 are foreign keys in the table.
$orginal_arr =
Array
(
[0] => Array
(
[id] => 17
[content] => string...?
)
[1] => Array
(
[id] => 20
[content] => hello
)
[2] => Array
(
[id] => 20
[content] => string...?
)
[3] => Array
(
[id] => 20
[content] => string...string...??
)
[4] => Array
(
[id] => 17
[content] => string...
)
[5] => Array
(
[id] => 17
[content] => string...
)
);
I want to convert the above $orginal_arr array to the following $desired_arr array.
$desired_arr = Array (
[0] => Array
(
[17] => Array
(
[0] => string...?
)
)
[1] => Array
(
[20] => Array
(
[0] => hello
[1] => string...?
[2] => string... string...??
)
)
[2] => Array
(
[17] => Array
(
[0] => string...
[1] => string...
)
)
);
so far I am trying the following approach:
function group_by_key($key, $data)
{
$result = array();
$j = 0;
foreach ($data as $val) {
if (array_key_exists($key, $val)) {
$result[$val[$key]][$j] = $val;
} else {
$result[""][] = $val;
}
$j++;
}
return $result;
}
$desired_arr = group_by_key("id", $orginal_arr);
$desired_arr2 = array();
foreach ($desired_arr as $index => $item) {
$desired_arr2 += $item;
}
echo "<pre>";
print_r($desired_arr2);
echo "</pre>";
the output:
Array
(
[0] => Array
(
[id] => 17
[content] => string...?
)
[4] => Array
(
[id] => 17
[content] => string...
)
[5] => Array
(
[id] => 17
[content] => string...
)
[1] => Array
(
[id] => 20
[content] => hello
)
[2] => Array
(
[id] => 20
[content] => string...?
)
[3] => Array
(
[id] => 20
[content] => string...string...??
)
)
- Thank you very much.
As I said in my comment, I'm not familiar to PHP syntax anymore so this will be pseudo code.
I would do it in 2 steps,
first I want this result
$temp_arr =
Array
(
[0] => Array
(
[id] => 17
[contents] => Array(
[0] => string...?
)
)
[1] => Array
(
[id] => 20
[contents] => Array(
[0] => hello
[1] => string...?
[2] => string...string...??
)
)
...
...
);
See, I kept the Object structure with id and contents (with a final S).
then it will be easier to check the id as it is directly accessible
$temp_array = Array();
foreach ($original_array as $element) {
$last = end($temp_array);
if ($last && $element->id == $last->id) {
// add to the last $temp_array element
$temp_array[count($temp_array)-1]->contents[] = $element->content;
}
else {
// create a new $temp_array element
$temp_array[] = Array(
"id" => $element->id,
"contents => Array(
$element->content
)
);
}
}
For the second step you just need to iterate the $temp_array and format as you like
here is another solution to this problem.
$desired_arr = array();
$prev_id = '';
$prev_key = null;
foreach ($orginal_arr as $key => $value) {
$this_id = $value['id'];
if ($this_id == $prev_id) {
array_push($desired_arr[$prev_key][$value['id']], $value['content']);
} else {
$desired_arr[$key][$value['id']] = (array)$value['content'];
$prev_key = $key;
}
$prev_id = $this_id;
}
$desired_arr = array_values($desired_arr);

display specific index multidimensional array

i have an multidimensional array like this, but i just only need some index to be displayed,
Array
Array
(
[0] => Array
(
[1] => 220
[38] => 200
[232] => 970
)
[1] => Array
(
[0] => 220
[2] => 190
[39] => 200
)
[2] => Array
(
[1] => 190
[3] => 40
[50] => 220
)
[3] => Array
(
[2] => 40
[4] => 200
[57] => 120
)
)
then i just want to display only index [1] and [3], so it would be like this
Array
(
[1] => Array
(
[0] => 220
[2] => 190
[39] => 200
)
[3] => Array
(
[2] => 40
[4] => 200
[57] => 120
)
)
i try using this code
$order = array(1,3);
uksort($graph, function($key1, $key2) use ($order) {
return (array_search($key1, $order) > array_search($key2, $order));
});
but still, its displayed the rest array that i dont need it that's key [0] and [2]
Like this:
foreach(array_keys($graph) as $key)
{
if($key == 0|| $key == 2)
{
unset($graph[$key]);
}
}
print_r
Array
(
[1] => Array
(
[0] => 220
[2] => 190
[39] => 200
)
[3] => Array
(
[2] => 40
[4] => 200
[57] => 120
)
)

Getting nids using entityfieldquery

I want to get all the nids but I am unable to get them. Below is my snippet.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->entityCondition('bundle', 'product');
$query->propertyCondition('status', 1);
$query->fieldCondition('field_product_sub_cat', 'tid',array($leaf_nodes));
$query->range(0, 10);
$result = $query->execute();
if (isset($result['node']))
{
$product_childnid = array_keys($result['node']);
}
Below is my result:UPDATED
Array
(
[node] => Array
(
[78] => stdClass Object
(
[nid] => 78
[vid] => 78
[type] => product
)
[80] => stdClass Object
(
[nid] => 80
[vid] => 80
[type] => product
)
[82] => stdClass Object
(
[nid] => 82
[vid] => 82
[type] => product
)
[84] => stdClass Object
(
[nid] => 84
[vid] => 84
[type] => product
)
[86] => stdClass Object
(
[nid] => 86
[vid] => 86
[type] => product
)
[88] => stdClass Object
(
[nid] => 88
[vid] => 88
[type] => product
)
[90] => stdClass Object
(
[nid] => 90
[vid] => 90
[type] => product
)
[92] => stdClass Object
(
[nid] => 92
[vid] => 92
[type] => product
)
[94] => stdClass Object
(
[nid] => 94
[vid] => 94
[type] => product
)
[100] => stdClass Object
(
[nid] => 100
[vid] => 100
[type] => product
)
)
)
then below code is providing me all the paths too,
$paths = array();
if (isset($result['node']))
{
foreach($result['node'] as $key => $node)
{
$paths[$node->nid] = drupal_get_path_alias("node/$node->nid");
}
}
now the next problem is how can I represent these paths in my anchor tag?Because they all are in an array.Below is my array structure of paths aliases:
Array
(
[78] => equivalent-fireman-glove-details
[80] => toughguard®-2
[82] => toughguard®-3
[84] => toughguard-®sizzler®
[86] => tough-guard-®
[88] => kevlar®-double-glove
[90] => nomex®-industrial-coverall
[92] => tough-guard®-clinker®-0
[94] => lab-coats-sa-nlc
)
and below is an array structure data to link with above paths:
Array
(
[0] => 71
[1] => 73
[2] => 81
[3] => 83
[4] => 85
[5] => 87
[6] => 89
[7] => 91
[8] => 93
)
Try this:
$query->fieldCondition('field_product_sub_cat', 'tid',array($leaf_nodes),'IN');
So that the query in compare value with array.

print array multidimensional values

i I have a problem to print array .I received this array of web services by nusoap
How can I display the values ​​of this multidimensional array. I worked with foreach, I could not do it., Please explain with an example. Thanks
my array:
Array ( [Result] => Array ( [Root] => Array ( [row] => Array ( [0] => Array ( [!R] => 0 [!C1] => 300064 [!C2] => name1 [!C3] => 1287744941 [!C4] => 798 [!C5] => 1338/06/29 [!C6] => [!C7] =>name2 [!C8] =>name2 91 ) [1] => Array ( [!R] => 19 [!C1] => 300064 [!C2] => name1 [!C3] => 1287744941 [!C4] => 798 [!C5] => 1338/06/29 [!C6] => [!C7] =>name2 [!C8] =>name2 92 ) [2] => Array ( [!R] => 38 [!C1] => 300064 [!C2] => name1 [!C3] => 1287744941 [!C4] => 798 [!C5] => 1338/06/29 [!C6] => [!C7] =>name2 [!C8] =>name2 93 ) [3] => Array ( [!R] => 57 [!C1] => 300064 [!C2] => name1 [!C3] => 1287744941 [!C4] => 798 [!C5] => 1338/06/29 [!C6] => [!C7] => name3 [!C8] => name3 ) ) ) ) )

How to perform array manipulation

I have an array like this from cdbcommand select query. (Using Yii)
Array(
[0] => Array(
[0] => Array
(
[id] => 21
)
[1] => Array
(
[id] => 91
)
[2] => Array
(
[id] => 125
)
)
[1] => Array
(
[0] => Array
(
[id] => 15
)
)
)
I want final array like array(21, 91, 125, 15)
How can I do this?
$output=array();
foreach($result as $row){
foreach($row as $id_container){
$output[]=$id_container["id"];
}
}
This will give you what you need, if array structure is dependable - then it wont work.
try this
$newArray=array();
foreach($result as $value)
{
array_merge($newArray,array_values($value));
}

Resources