0

So, i am trying to display a chart where the profit and payout is rendering on a chart. If there is a no profit and payout value in chart then it should show 0.

I have an array with values of profit and payout with hour. Now i want to replace that array with an existing array of 0 values.

Here is my code

$a1=array();
for($i=0,$i<=24,$i++){
  $a1['hour']=$i;
  $a1['payout']='0';
  $a1['profit']='0';
}

$a2=array();
$a2['hour']='2';
$a2['profit']='300';
$a2['payout']='100';
print_r(array_replace($a1,$a2));

There is something wrong with this code. Can any1 tell me what i am doing wrong?

7
  • Whats the purpose of forloop? In the end $a1['hour'] will be 24 always. Commented May 23, 2017 at 16:06
  • @PriyeshKumar Thanks for highlighting Commented May 23, 2017 at 16:14
  • Please edit the question and add expected output Commented May 23, 2017 at 16:18
  • The first array should have hours from 1 to 24 and the profit and payout values should be 0 and after replacing with array 2. The out should contain all the values from array 1 and 0 values should be replaced with the matching values from array 2 Commented May 23, 2017 at 16:24
  • $a1['hour'] cannot take multiple values. Thats why i asked whats the purpose of forloop. It will be better if you can write output like output: Array( 0=> something)(not in words) Commented May 23, 2017 at 16:25

3 Answers 3

1
<?php

// Initial array
$a1=array();

for($i=0;$i<=24;$i++){

    // Use hour as index of array, if you use $a2[] = array(), it works
    // But problem is when you change hours, lets say 12-24, if will cause problem

    $a1[$i] = array(
        'hour'=> $i,
        'payout'=> 0,
        'profit'=> 0
    );
}

// Array from database
$a2=array();
$a2[] = array(
    'hour'=> 2,
    'payout'=> 300,
    'profit'=> 100
);
$a2[] = array(
    'hour'=> 5,
    'payout'=> 3500,
    'profit'=> 1200
);

echo '<pre>';

// Loop through second array and check if it is there in first one.
foreach( $a2 as $item) {
    if(isset($a1[$item['hour']])) {
        // Replace the values
        $a1[$item['hour']] = $item;
    }
}
print_r($a1);
?>

You are using for loop in wrong way, SyntaxError

for($i=0;$i<=24;$i++){ // <= See semi colons

}
Sign up to request clarification or add additional context in comments.

2 Comments

$a1=array(); for($i=0;$i<=24;$i++){ $a1[] = array('hour' => $i); $a1[] = array('payout' => '0'); $a1[] = array('hour' => '0'); } $a2=array(); $a2['hour']='2'; $a2['profit']='300'; $a2['payout']='100'; print_r(array_replace($a1,$a2)); i just changed my code and i am not getting the desired output. can you help ?
Thanks for helping me
0

First of all your For Loop isn't correct ! you must replace the "," with ";"

Comments

0

You are having a syntax error in your program.

Your working program should look something like this

    $a1=array();
    for($i=0;$i<=24;$i++){
      $a1['hour']=$i;
      $a1['payout']='0';
      $a1['profit']='0';
    }

    $a2=array();
    $a2['hour']='2';
    $a2['profit']='300';
    $a2['payout']='100';
    print_r(array_replace($a1,$a2));

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.