Assume that applicant id was passed from the other form.I have array variable coming from my database, here is the code :
$array_id_applicants = explode(";",stripslashes($applicant_id1));
$applicants_num = count($array_id_applicants);
$arr_app_num = array();
for($x=0;$x<=$applicants_num;$x++)
{
if($array_id_applicants[$x]){
$applicant_id = str_replace("'","",$array_id_applicants[$x]);
$applicants = getdata("select cellphone from personal where applicant_id='".$applicant_id."'");
$replace_array = array("-","(",")","+","_");
array_push($arr_app_num,str_replace($replace_array,"",$applicants[1][cellphone]));
}
}
$applicant_number = implode(";",$arr_app_num);
echo $applicant_number; exit;
Assume that this is the value of array :
$applicant_number = '639152478931 / 631687515455','631235497891'
I want the output to be like this :
$applicant_number = '639152478931','631687515455','631235497891'
See if this is what you are trying to do.:
<?php
$applicant_number[] = '639152478931 / 631687515455';
$applicant_number[] = '631235497891';
$applicant_number[] = '0294765388389 / 52525252525';
$applicant_number[] = '0012324252728';
$new = array();
foreach($applicant_number as $number) {
if(strpos($number,'/') !== false) {
$val = explode("/",str_replace(" ","",$number));
$new = array_merge($new,$val);
}
else
$new[] = $number;
}
print_r($new);
?>
Gives you:
Array
(
[0] => 639152478931
[1] => 631687515455
[2] => 631235497891
[3] => 0294765388389
[4] => 52525252525
[5] => 0012324252728
)
<?php
$applicant_number[] = '639152478931';
$applicant_number[] = '631235497891';
$applicant_number[] = '1111111110294765388389';
$applicant_number[] = '0012324252728';
$new = array();
foreach($applicant_number as $number) {
$count = strlen($number) - 10;
$b = substr($number,$count);
$new[] = "+63".$b;
}
print_r($new);
?>
Gives you :
Array ( [0] => +639152478931 [1] => +631235497891 [2] => +634765388389 [3] => +632324252728 )
Related
I have an array which as dynamic nested indexes in e.g. I am just using 2 nested indexes.
Array
(
[0] => Array
(
[0] => 41373
[1] => 41371
[2] => 41369
[3] => 41370
)
[1] => Array
(
[0] => 41378
[1] => 41377
[2] => 41376
[3] => 41375
)
)
Now I want to create a single array like below. This will have 1st index of first array then 1st index of 2nd array, 2nd index of first array then 2nd index of 2nd array, and so on. See below
array(
[0] =>41373
[1] => 41378
[2] => 41371
[3] => 41377
[4] => 41369
[5] => 41376
[6] => 41370
[7] => 41375
)
You can do something like this:
$results = [];
$array = [[1,2,3,4], [1,2,3,4], [1,2,3,4]];
$count = 1;
$size = count($array)-1;
foreach ($array[0] as $key => $value)
{
$results[] = $value;
while($count <= $size)
{
$results[] = $array[$count][$key];
$count++;
}
$count = 1;
}
I think you need something like this:
function dd(array $arrays): array
{
$bufferArray = [];
foreach($arrays as $array) {
$bufferArray = array_merge_recursive($bufferArray, $array);
}
return $bufferArray;
}
$array1 = ['41373','41371','41369','41370'];
$array2 = ['41378','41377', '41376', '41375'];
$return = array();
$count = count($array1)+count($array2);
for($i=0;$i<($count);$i++){
if($i%2==1){
array_push($return, array_shift($array1));
}
else {
array_push($return, array_shift($array2));
}
}
print_r($return);
first count the arrays in the given array, then count the elements in the first array, than loop over that. All arrays should have the same length, or the first one should be the longest.
$laArray = [
['41373','41371','41369','41370'],
['41378', '41377', '41376', '41375'],
['43378', '43377', '43376', '43375'],
];
$lnNested = count($laArray);
$lnElements = count($laArray[0]);
$laResult = [];
for($lnOuter = 0;$lnOuter < $lnElements; $lnOuter++) {
for($lnInner = 0; $lnInner < $lnNested; $lnInner++) {
if(isset($laArray[$lnInner][$lnOuter])) {
$laResult[] = $laArray[$lnInner][$lnOuter];
}
}
}
this would be the simplest solution:
$firstarr = ['41373','41371','41369','41370'];
$secondarr = ['41378','41377','41376','41375'];
$allcounged = count($firstarr)+count($secondarr);
$dividedintotwo = $allcounged/2;
$i = 0;
while ($i<$dividedintotwo) {
echo $firstarr[$i]."<br>";
echo $secondarr[$i]."<br>";
$i++;
}
I have this array
dev3->content->->mktg->->->pls1->->->pls2->->->config->->splash
I want to convert this string to multidimensional array. like this
Array
(
[0] => dev3
Array (
[0] => ->content
Array (
[0] => ->->mktg
Array(
[0] => ->->->pls1
[1] => ->->->pls2
[2] => ->->->config
)
[1] => ->->splash
)
)
)
Can anyone do this
it does not work if level will be increaed more then +1 on any step
$str = 'dev3->content->->mktg->->->pls1->->->pls2->->->config->->splash';
$in = preg_split('/(?<!>)(?=->)/', $str);
Above we make such array from the input string
Array
(
[0] => dev3
[1] => ->content
[2] => ->->mktg
[3] => ->->->pls1
[4] => ->->->pls2
[5] => ->->->config
[6] => ->->splash
)
continue working
$result = [];
$p = &$result;
$level = 0;
foreach($in as $i) {
// Count next level
$c = substr_count($i, '->');
// if level is not changed
if($c == $level) { $p[] = $i; continue; }
// level increased
if ($c == $level + 1) {
$level++;
$p[] = [$i];
$p = &$p[count($p)-1];
continue;
}
// any level less then achived before
if ($c < $level) {
$p = &$result;
$level = $c;
while($c--)
$p = &$p[count($p)-1];
$p[] = $i;
continue;
}
die("I can't process this input string");
}
print_r($result);
working demo
I'm trying to create foreach statement using multidimensional array.
Controller:
function index()
{
$index1 = 0;
$index2 = 0;
$index3 = 0;
$index4 = 0;
$result1 = $this->data->get_test('kdprogram','kdprogram');
foreach($result1 as $row1){
$array_temp[$index1] = $row1;
$result2 = $this->data->get_test('kdgiat','kdgiat','kdprogram = '.$row1['kdprogram']);
foreach($result2 as $row2){
$array_temp[$index1][$index2] = $row2;
$result3 = $this->data->get_test('kdoutput','kdoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat']);
foreach($result3 as $row3){
$array_temp[$index1][$index2][$index3] = $row3;
$result4 = $this->data->get_test('kdsoutput','kdsoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat'] .' and kdoutput = '.$row3['kdoutput']);
foreach($result4 as $row4){
$array_temp[$index1][$index2][$index3][$index4] = $row4;
$index4++;
}
$index3 ++;
}
$index2 ++;
}
$index1 ++;
}
//print_r($array_temp);
$data['damn'] = $array_temp;
$this->load->view('report/laporan_output', $data);
}
$data contains:
Array
(
[0] => Array
(
[kdprogram] => 06
[0] => Array
(
[kdgiat] => 3400
[0] => Array
(
[kdoutput] => 001
[0] => Array
(
[kdsoutput] => 001
)
[1] => Array
(
[kdsoutput] => 006
)
)
[1] => Array
(
[kdoutput] => 008
[2] => Array
(
[kdsoutput] => 001
)
)
)
)
)
How to echo each array (kdprogram, kdgiat, etc) on view especially with html table?
Am i doing it right?
Thanks
it looks kinda ugly and i would use some sort of recursive function but here is your way
in controller ( i assume the arrays have numeric index otherwise you have to use some sort of counter like you did in your code )
foreach($result1 as $a_counter=>$row1)
{
$array_temp[$a_counter] = array( 'parent'=>$row1 , 'child'=>array());
$result2 = $this->data->get_test('kdgiat','kdgiat','kdprogram = '.$row1['kdprogram']);
foreach($result2 as $b_counter=> $row2)
{
$array_temp[$a_counter]['child'][$b_counter] = array( 'parent'=>$row2 , 'child'=>array());
$result3 = $this->data->get_test('kdoutput','kdoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat']);
foreach($result3 as $c_counter=>$row3)
{
$array_temp[$a_counter]['child'][$b_counter]['child'][$c_counter] = array( 'parent'=>$row3 , 'child'=>array());
$result4 = $this->data->get_test('kdsoutput','kdsoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat'] .' and kdoutput = '.$row3['kdoutput']);
foreach($result4 as $row4)
{
$array_temp[$a_counter]['child'][$b_counter]['child'][$c_counter]['child'][] = $row;
}
}
}
}
in the view
foreach($result as $a )
{
// show a
foreach($a['child'] as $b )
{
// show b
foreach($b['child'] as $c )
{
// show c
foreach($c['child'] as $d )
{
// show d
}
}
}
}
I have a functon that is passed an array of url's. I am extracting data from each webpage and then assigning each piece of data to an array. Here's my function:
function getitems ($urls) {
$iteminfo = array();
foreach($urls as $link) {
$circdl = my_curl($link);
$circqp = htmlqp($circdl,'body');
$itemtitle = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
$itemlink = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
$itemdesc = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
$iteminfo[][] = $itemtitle;
//$iteminfo[$itemtitle][] = $itemlink;
//$iteminfo[$itemtitle][] = $itemdesc;
}
return $iteminfo;
}
I want the array to look like this:
Array ( [0] => Array ( [0] => title [1] => link [2] => desc ) [1] => Array ( [0] => title [1] => link [2] => desc ) [2] => Array ( [0] => title [1] => link [2] => desc ) )
But I can't wrap my head around how to additional fields to the sub-arrays.
try something like this
function getitems ($urls) {
$iteminfo = array();
$i = 0;
foreach($urls as $link) {
$circdl = my_curl($link);
$circqp = htmlqp($circdl,'body');
$itemtitle = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
$itemlink = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
$itemdesc = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
$iteminfo[$i][] = $itemtitle;
$iteminfo[$i][] = $itemlink;
$iteminfo[$i][] = $itemdesc;
$i++;
}
return $iteminfo;
}
Everything is ok, you just have to assign index to each of your rows.
If i understand you correctly...
$iteminfo[] = array($itemtitle, $itemlink, $itemdesc);
function getitems ($urls) {
$iteminfo = array();
foreach($urls as $link) {
$subInfo = array();
$circdl = my_curl($link);
$circqp = htmlqp($circdl,'body');
$subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
$subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
$subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
$iteminfo[] = $subInfo;
}
return $iteminfo;
}
You could easily replace
$iteminfo[][] = $itemtitle;
//$iteminfo[$itemtitle][] = $itemlink;
//$iteminfo[$itemtitle][] = $itemdesc;
with
$iteminfo = array($itemtitle, $itemlink, $itemdesc);
You can do this because the syntax
$array = $element; // where $array = array();
is just another way to add element to an array in PHP and $element can be an array() as well.
How do you convert an array in PHP that looks like this:
Array (
[2] => B.eot
[3] => B.ttf
[4] => CarnevaleeFreakshow.ttf
[5] => CarnevaleeFreakshow.eot
[6] => TRASHED.ttf
[7] => sub.ttf
)
To look like this:
Array(
[B]=>array(
[0] => B.eot
[1] => B.ttf
)
[CarnevaleeFreakshow]=>array(
[0] => CarnevaleeFreakshow.ttf
[1] => CarnevaleeFreakshow.eot
)
[TRASHED]=>array(
[0] => TRASHED.ttf
)
[sub]=>array(
[0] => sub.ttf
)
)
Is there a name for doing something like this?
the data is being retrieved from a
scandir
array.
<?php
$data = array (
2 => 'B.eot',
3 => 'B.ttf',
4 => 'CarnevaleeFreakshow.ttf',
5 => 'CarnevaleeFreakshow.eot',
6 => 'TRASHED.ttf',
7 => 'sub.ttf'
);
$new_data = array();
foreach ( $data as $value ) {
$tmp = explode( '.', $value );
$ext = '';
if ( $tmp[1] ) $ext = '.' . $tmp[1];
$new_data[ $tmp[0] ][] = $tmp[0] . $ext;
}
print_r( $new_data );
?>
Here is an example.
It can be written shorter, but I think this is the most instructive.
$ARRraw = array (
"B.eot",
"B.ttf",
"CarnevaleeFreakshow.ttf",
"CarnevaleeFreakshow.eot",
"TRASHED.ttf",
"sub.ttf"
) ;
$sorted = array();
foreach($ARRraw as $one){
$firstPoint = strpos($one,".");
// No point? then skip.
if (!($firstPoint === false)){
// Get the part before the point.
$myKey = substr($one,0,$firstPoint);
$sorted[$myKey][] = $one;
}
}
Is there a name for doing something like this?
Nope. Anyway, it should be rather simple using a loop:
<?php
$newArray = array( );
foreach( $originalArray as $fontfile ) {
$newArray[basename( $font )][] = $fontfile;
}
echo '<pre>' . print_r( $newArray, true );
To what i know there is no 'simple' method of doing this.
you could build a function to handle it though.
function convertArray($array) {
$newArray = array();
foreach( $array as $item ) {
$newArray[basename($item)] = $item;
}
return $newArray;
}
That should do what your looking for.
Try it:
function subdiv(array $arr) {
$res = array();
foreach($arr as $val) {
$tmp = explode('.', $val);
if(!isset($res[$tmp[0]]))
$res[$tmp[0]] = array();
$res[$tmp[0]][] = $val;
} return $res;
}
use with:
$res = subdiv($array);
var_dump($res);