PHP Divide Array By Empty Value - arrays

This is an array from input field as below
Array(
[0] =>
[1] => 737
[2] => 736
[3] => 735
[4] =>
[5] => 748
[7] => 744
[8] => 747
[9] => 746
[10] =>
[11] => 748
[12] => 747
[13] => 746
[14] => 745
[15] => 744
);
Is there a way to divide it by empty value and removing the empty value as below:
Array(
[0] => Array(
[0] => 737
[1] => 736
[2] => 735
)
[1] => Array(
[0] => 748
[1] => 744
[2] => 747
[3] => 746
)
[2] => Array(
[0] => 748
[1] => 747
[2] => 746
[3] => 745
[4] => 744
)
)

If you want to split the array into sub-arrays with the "null" values as delimiters, something like that should do the job:
$newArray = array();
$subArray = null;
for ($i = 0; $i < count($array); $i++) {
if (!$subArray) $subArray = array();
$v = $array[$i];
if ($v) {
array_push($subArray, $v);
} else {
if (count($subArray) > 0) array_push($newArray, $subArray);
$subArray = null;
}
}
if ($subArray) array_push($newArray, $subArray);

Related

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
)
)

Merge two arrays like this

I have two array like this::
$doctor = Array(
[0] => 4
[1] => 5
[2] => 8
[3] => 35
[4] => 41
[5] => 42
)
$clinic = Array(
[0] => 1
[1] => 3
[2] => 9
[3] => 15
[4] => 19
[5] => 20
)
Now i want to add these array like this
$all = array(
[0] => 4
[1] => 1
[2] => 5
[3] => 3
[4] => 8
[5] => 9
[6] => 35
[7] => 15
[8] => 41
[9] => 19
[10] => 42
[11] => 20
I have tried this but it is not my expected output:
$all = array_merge( $doctor , $clinic );
Any solution?
Thanks
You can use for loop to do that
$all=[];
for($i=0;$i<6;$i++){
$all[]=$doctor[$i];
$all[]=$clinic[$i];
}
if you don't have same length for the arrays,
Try
$doctor_size=sizeof($doctor);
$clinic_size=sizeof($clinic);
$all=[];
$size=$doctor_size;
if($doctor_size<$clinic_size){
$size=$clinic_size;
}
for($i=0;$i<$size;$i++){
if(isset($doctor[$i])){
$all[]=$doctor[$i];
}
if(isset($clinic[$i])){
$all[]=$clinic[$i];
}
}

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.

PHP - remove element in multidimensional array

I have this array:
Array (
[0] => Array ( [0] => b [1] => d [2] => **c** [3] =>a [4] => )
[1] => Array ( [0] => **c** [1] => a [2] => d [3] => [4] => )
[2] => Array ( [0] => b [1] => d [2] => a [3] => [4] => )
[3] => Array ( [0] => **c** [1] => d [2] => a [3] =>b [4] => )
)
and need to delete (unset?) all elements where value is "c" so that one ends up with:
Array (
[0] => Array ( **[0] => b [1] => d [2] => a [3] => [4] =>** )
[1] => Array ( **[0] => a [1] => d [2] => [3] =>** )
[2] => Array ( [0] => b [1] => d [2] => a [3] => [4] => )
[3] => Array ( **[0] => d [1] => a [2] =>b [3] =>** )
)
The element gets removed, and the other elements to shift up. I know that unset does not re-index the array. Cannot get to unset for all multidimensional arrays, but only with one array. Can the arrays be re-indexed afterwards? Appreciate it.
The code BELOW removes elements where the value is equal to "c" but the index of the first element is not re-indexed. Can anyone suggest a solution to re-indexing the inner arrays?
$i=0;
foreach ($array as $val)
{
foreach ($val as $key => $final_val)
{
if ($final_val =="$search_value")
{
unset($array[$i][$key]);
}
}
i = $i + 1;
}
The following code will do what you want:
<?php
$a = 1;
$b = 2;
$c = 3;
$d = 4;
$arr = array(
array ( $b, $d, $c, $a, $b),
array ($c, $a),
array ( $b, $d, $c ),
array( $c, $d, $a, $b, $b)
);
echo "before:\n";
print_r($arr);
foreach($arr as $k1=>$q) {
foreach($q as $k2=>$r) {
if($r == $c) {
unset($arr[$k1][$k2]);
}
}
}
echo "after:\n";
print_r($arr);
?>
Output:
before:
Array
(
[0] => Array
(
[0] => 2
[1] => 4
[2] => 3
[3] => 1
[4] => 2
)
[1] => Array
(
[0] => 3
[1] => 1
)
[2] => Array
(
[0] => 2
[1] => 4
[2] => 3
)
[3] => Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 2
[4] => 2
)
)
after:
Array
(
[0] => Array
(
[0] => 2
[1] => 4
[3] => 1
[4] => 2
)
[1] => Array
(
[1] => 1
)
[2] => Array
(
[0] => 2
[1] => 4
)
[3] => Array
(
[1] => 4
[2] => 1
[3] => 2
[4] => 2
)
)
As you can see, all the 3's have gone...
Search the value in the sub array then unset it.
$search = 'c';
$result = array_map(function ($value) use ($search) {
if(($key = array_search($search, $value)) !== false) {
unset($value[$key]);
}
return $value;
}, $your_array);
Or you could use a loop too:
// this way change your original array
foreach ($your_array as &$sub_array) {
if(($key = array_search($search, $sub_array)) !== false) {
unset($sub_array[$key]);
}
}
var_dump($your_array);

Zend Framework Multi Database issue

application.ini
resources.multidb.db1.adapter = "pdo_mysql"
resources.multidb.db1.host = "localhost"
resources.multidb.db1.username = "root"
resources.multidb.db1.password = "root"
resources.multidb.db1.charset = "utf8"
resources.multidb.db1.dbname = "admin"
resources.multidb.db2.adapter = "pdo_mysql"
resources.multidb.db2.host = "localhost"
resources.multidb.db2.username = "root"
resources.multidb.db2.password = "root"
resources.multidb.db2.dbname = "hp"
resources.multidb.db2.charset = "utf8"
resources.multidb.db2.default = true
Bootstrap.php:
protected function _initDb(){
$resources = $this->getPluginResources('multidb');
Zend_Registry::set('db1', $resource->getDb('db1'));
Zend_Registry::set('db2', $resource->getDb('db2'));
}
class methods:
Array
(
[0] => _initDb
[1] => _initPrefix
[2] => _initSession
[3] => _initRouters
[4] => _initAutoloader
[5] => _initControllerHelper
[6] => __construct
[7] => run
[8] => setResourceLoader
[9] => getResourceLoader
[10] => getAppNamespace
[11] => setAppNamespace
[12] => setOptions
[13] => getOptions
[14] => hasOption
[15] => getOption
[16] => mergeOptions
[17] => getClassResources
[18] => getClassResourceNames
[19] => registerPluginResource
[20] => unregisterPluginResource
[21] => hasPluginResource
[22] => getPluginResource
[23] => getPluginResources
[24] => getPluginResourceNames
[25] => setPluginLoader
[26] => getPluginLoader
[27] => setApplication
[28] => getApplication
[29] => getEnvironment
[30] => setContainer
[31] => getContainer
[32] => hasResource
[33] => getResource
[34] => __get
[35] => __isset
[36] => bootstrap
[37] => __call
[38] => _bootstrap
[39] => _executeResource
[40] => _loadPluginResource
[41] => _markRun
[42] => _resolvePluginResourceName
)
When i run application, its return error: Call to a member function getDb() on a non-object.
Can anyone help me?
Thanks!
Sorry my English is bad.
You had typo in your function -
protected function _initDb(){
$resources = $this->getPluginResources('multidb');
// changed $resource to $resources
Zend_Registry::set('db1', $resources->getDb('db1'));
Zend_Registry::set('db2', $resources->getDb('db2'));
}

Resources